diff --git a/Rakefile b/Rakefile index 8f048beb..fc218d94 100644 --- a/Rakefile +++ b/Rakefile @@ -238,3 +238,21 @@ task :generate_sitemap => [:environment] do gz.write %{} end end + +desc 'dedupe tags' +task :dedupetags => [:environment] do + Tag.all.each do |tag| + begin + tag.reload + rescue Sequel::Error => e + next if e.message =~ /Record not found/ + end + + matching_tags = Tag.exclude(id: tag.id).where(name: tag.name).all + + matching_tags.each do |matching_tag| + DB[:sites_tags].where(tag_id: matching_tag.id).update(tag_id: tag.id) + matching_tag.delete + end + end +end \ No newline at end of file diff --git a/models/site.rb b/models/site.rb index 5a7ae69d..fc5f6aeb 100644 --- a/models/site.rb +++ b/models/site.rb @@ -904,7 +904,7 @@ class Site < Sequel::Model end def add_tag_name(name) - add_tag Tag[name: name] || Tag.create(name: name) + add_tag Tag.find_or_create(name: name) end def before_create