Batch tagging through console


#1

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 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!


(Alan Tan) #2

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.

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

Adding a tag in bulk without erasing existing tags
#3

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:

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

(Chris Croome) #4

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)?

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

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

(Chris Croome) #5

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

#6

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 button and then you should see checkboxes. Now you can select which ones you want to change/append to, and then click the image button and you should see a menu with some options.

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


(Joshua Rosenfeld) #7

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)

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)

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

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

#8

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 )

https://meta.discourse.org/t/batch-tagging-through-console/47602

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


(Joshua Rosenfeld) #9

This code in post 5 should work for that - does it not work for you?


#10

Hello,

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


(Joshua Rosenfeld) #11

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