Skip to content

Commit

Permalink
Merge pull request #839 from optimalworkshop/find_or_create_all_optim…
Browse files Browse the repository at this point in the history
…ization

Avoid unnecessary queries when finding or creating tags
  • Loading branch information
seuros committed Feb 8, 2019
2 parents 4794c91 + d43736f commit fbf2b60
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/acts_as_taggable_on/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ def self.find_or_create_all_with_like_by_name(*list)

return [] if list.empty?

existing_tags = named_any(list)
list.map do |tag_name|
begin
tries ||= 3

existing_tags = named_any(list)
comparable_tag_name = comparable_name(tag_name)
existing_tag = existing_tags.find { |tag| comparable_name(tag.name) == comparable_tag_name }
existing_tag || create(name: tag_name)
rescue ActiveRecord::RecordNotUnique
if (tries -= 1).positive?
ActiveRecord::Base.connection.execute 'ROLLBACK'
existing_tags = named_any(list)
retry
end

Expand Down

0 comments on commit fbf2b60

Please sign in to comment.