Skip to content

Commit

Permalink
Merge pull request #19475 from lpichler/update_validate_uniqueness_on…
Browse files Browse the repository at this point in the history
…_tag_name

Reduce queries in tag name validation in Classification model
  • Loading branch information
kbrock authored Jan 3, 2020
2 parents 73965d3 + 913afe5 commit 6bf7f94
Showing 1 changed file with 5 additions and 8 deletions.
13 changes: 5 additions & 8 deletions app/models/classification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -504,14 +504,11 @@ def self.add_entries_from_hash(cat, entries)
private_class_method :add_entries_from_hash

def validate_uniqueness_on_tag_name
tag = find_tag
return if tag.nil?
cond = ["tag_id = ?", tag.id]
unless new_record?
cond[0] << " and id <> ?"
cond << id
end
errors.add("name", "has already been taken") if Classification.exists?(cond)
tag_name = Classification.name2tag(name, parent, ns)
exist_scope = Classification.includes(:tag).where(:tags => {:name => tag_name}).merge(Tag.in_region(region_id))
exist_scope = exist_scope.where.not(:id => id) unless new_record?

errors.add("name", "has already been taken") if exist_scope.exists?
end

def self.name2tag(name, parent_id = nil, ns = DEFAULT_NAMESPACE)
Expand Down

0 comments on commit 6bf7f94

Please sign in to comment.