Bulk tagging topics based on keywords

AI-generated summary

The discussion revolves around bulk tagging topics based on specific keywords. bartv wants to apply a tag to topics containing certain words in the title or any post within the topic, using a logic of “IF topic title or post contains word-1, word-2, etc. THEN apply tag.” pfaffman provided a starting point by suggesting the use of Post.where to find matching posts, and then creating a tag for each match, recommending the use of arrays and loops for multiple keywords.

Hi,

In order to use the new ‘no ads for tags’ support in the ad plugin, we need to mass-tag any ‘nswf’ topics on our forum based on specific keywords. For example, any topic that contains the word ‘nude’ in the title or any post in the topic needs to be tagged nsfw.

We have a list of such keywords, so the logic that I’m looking for is:

IF the topic title OR any post in this topic
CONTAINS word-1 or word-2 ..
THEN apply a tag to the topic

I found this topic, but my case seems a little more complex. Can someone help me out with a query to achieve this?

I’m on my phone. Here are some parts

   posts = Post.where("raw like '%badword%')
 posts.each do |post|
 Tag.create(some stuff here) # this won't work
 end

If there are lots of words, you’d stick them in an arrays and loop through them. You’ll need to find an example of what fields are required to create a tag. Probably topic_id and name.