Batch tagging through console

Sorry I should have played with this more before asking for help, the following appears to work if you create the tag on a post first:

category = Category.find_by(slug: 'testing')
tag = Tag.find_by(name: 'testing')

Topic.where(category: category).each do |topic|
  if topic.tags.exclude?(tag)
    topic.tags << tag
  end
end
7 Likes