Bulk tagging topics based on keywords


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

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.