# Batch tagging through console

**URL:** <https://meta.discourse.org/t/batch-tagging-through-console/47602>\
**Category:** Product Support\
**Created:** [July 21, 2016, 2:22pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602 "2016-07-21T14:22:52Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![mr8](https://avatars.discourse-cdn.com/v4/letter/m/f9ae1b/32.png) [@mr8](https://meta.discourse.org/u/mr8)\
**Post date:** [July 21, 2016, 8:12pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/3 "2016-07-21T20:12:17Z")

</div>

Thanks! That was exactly what I needed!

I only needed to make a minor change and add `if topic.tags.exclude?(tag) ` on the if statement to avoid duplicates.

Here’s what I used for future reference:

```plaintext
tag = Tag.find_by(name: 'TAG')
category = Category.find_by(name: 'CAT_NAME')

Topic.where(category: category).each do |topic|
  if topic.tags.exclude?(tag) && topic.first_post.raw =~ /<your keyword here>/
    topic.tags << tag
  end
end

```

---

_[View the full topic](https://meta.discourse.org/t/batch-tagging-through-console/47602)._
