# Batch tagging through console

**URL:** https://meta.discourse.org/t/batch-tagging-through-console/47602
**Category:** Support
**Created:** [21 Luglio 2016, 2:22pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602 "2016-07-21T14:22:52Z")
**Posts on this page:** 11
**Page:** 1

<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: [21 Luglio 2016, 2:22pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/1 "2016-07-21T14:22:52Z")

</div>

Hello, we just updated a bunch of the tags on our site and were wondering if it would be possible to batch tag a bunch of old topics that contain a certain keyword/url in the first post in a certain category through the console without updating the last edit time?

e.g. – Add a **soundcloud** tag to all topics in the **music** category if the first post contains a _[soundcloud.com](http://soundcloud.com)_ url, but without bumping all the last edit dates.

I’m not the most comfortable in the console, so any help would be great!  
Thanks!

---

<div class="post-metadata">

### Author: ![tgxworld](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/tgxworld/32/106117_2.png) [@tgxworld](https://meta.discourse.org/u/tgxworld)
#### Post date: [21 Luglio 2016, 2:38pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/2 "2016-07-21T14:38:41Z")

</div>

Something quick that I think will work. Might want to verify against a couple of records first before uncommenting the line to add the tag.

```plaintext
tag = Tag.find_by(name: 'soundcloud')
category = Category.find_by(name: 'music')

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

```

---

<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: [21 Luglio 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

```

---

<div class="post-metadata">

### Author: ![chrisc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisc/32/119982_2.png) [@chrisc](https://meta.discourse.org/u/chrisc)
#### Post date: [26 Ottobre 2017, 9:45am UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/4 "2017-10-26T09:45:07Z")

</div>

I’m totally new to the rails console and I need to do something like this; tag all the posts in 8 categories with the title of each category and then once that has been done move all the posts in these 8 categories into an “Archived” category.

Would something like this work for the tagging of the posts in each category (since there are only 8 categories it doesn’t seem worth writing a loop)?

```plaintext
category = Category.find_by(name: 'oldstuff')

Topic.where(category: category).each do |topic|
  topic.tags << oldstuff
end

```

---

<div class="post-metadata">

### Author: ![chrisc](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/chrisc/32/119982_2.png) [@chrisc](https://meta.discourse.org/u/chrisc)
#### Post date: [26 Ottobre 2017, 10:22am UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/5 "2017-10-26T10:22:39Z")

</div>

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:

```plaintext
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

```

---

<div class="post-metadata">

### Author: ![Berto](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/berto/32/77084_2.png) [@Berto](https://meta.discourse.org/u/Berto)
#### Post date: [29 Ottobre 2017, 5:44pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/6 "2017-10-29T17:44:13Z")

</div>

As an FYI, there is a bulk tag editing mode in the UI, but it was a bit tough for me to find. Go to `YOURSITE.com/tags/none` and then click the ![image](https://global.discourse-cdn.com/meta/original/3X/e/4/e4f1a00522d96574ce0f63c3144c141d73ff6afb.png) button and then you should see checkboxes. Now you can select which ones you want to change/append to, and then click the ![image](https://global.discourse-cdn.com/meta/original/3X/6/3/636968121a37ed3352b1c9c9962143f805598358.png) button and you should see a menu with some options.

These shouldn’t change the updated-timestamps on the posts or anything.

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [13 Dicembre 2017, 9:05pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/7 "2017-12-13T21:05:12Z")

</div>

To append a tag to every topic on a forum, first ensure the tag is used on at least one topic. Then run the following (replacing `testing` with the name of the tag)

```plaintext
tag = Tag.find_by(name: 'testing')

Topic.where(archetype: 'regular').find_each do |topic|
 if topic.tags.exclude?(tag)
   topic.tags << tag
 end
end

```

To append a tag to every topic on the forum _that does not have any tags already_, first ensure the tag is used on at least one topic. Then run the following (replacing `testing` with the name of the tag)

```plaintext
tag = Tag.find_by(name: 'testing')

Topic.where(archetype: 'regular').find_each do |topic|
  if topic.tags.count == 0
    topic.tags << tag
  end
end

```

---

<div class="post-metadata">

### Author: ![plosprawrs](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@plosprawrs](https://meta.discourse.org/u/plosprawrs)
#### Post date: [9 Febbraio 2018, 11:33pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/8 "2018-02-09T23:33:03Z")

</div>

Hello all,

I was trying to mass assign/append tags to topics based on category. I don’t really see a place to do this within the GUI that doesn’t take a long time. ( Select-all only works on what your screen has scrolled too. )

From the rails console I’ve been trying something like this ( old thread )

> [@Batch tagging through console](https://meta.discourse.org/t/batch-tagging-through-console/47602):
>
> Hello, we just updated a bunch of the tags on our site and were wondering if it would be possible to batch tag a bunch of old topics that contain a certain keyword/url in the first post in a certain category through the console without updating the last edit time? e.g. – Add a soundcloud tag to all topics in the music category if the first post contains a [soundcloud.com](http://soundcloud.com) url, but without bumping all the last edit dates. I’m not the most comfortable in the console, so any help would be great! T…

I’m currently on [v2.0.0.beta2 +5]

For instance, I’d simply like to assign tag “classic” to ALL topics within category “CLASSIC”.

Kind Regards

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [10 Febbraio 2018, 12:03am UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/9 "2018-02-10T00:03:44Z")

</div>

This code in [post 5](https://meta.discourse.org/t/batch-tagging-through-console/47602/5) should work for that - does it not work for you?

---

<div class="post-metadata">

### Author: ![plosprawrs](https://avatars.discourse-cdn.com/v4/letter/p/5daacb/32.png) [@plosprawrs](https://meta.discourse.org/u/plosprawrs)
#### Post date: [21 Marzo 2018, 6:54pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/10 "2018-03-21T18:54:25Z")

</div>

Hello,

my apologies, I meant to update the post. Thanks for your help, it did indeed work properly.

---

<div class="post-metadata">

### Author: ![jomaxro](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jomaxro/32/126216_2.png) [@jomaxro](https://meta.discourse.org/u/jomaxro)
#### Post date: [26 Marzo 2018, 10:00pm UTC](https://meta.discourse.org/t/batch-tagging-through-console/47602/11 "2018-03-26T22:00:08Z")

</div>

This topic was automatically closed after 33 hours. New replies are no longer allowed.
