Bulk move many topics from one category to another


(Tobias Eigen) #1

Continuing the discussion from Move topics to new category:

Is there a query to bulk move many topics from one category to another? I want to consolidate many categories into one category and would like to make the move quickly and all at once, and also not bump the topics.

Bonus: also want to tag topics first with a category-equivalent tag before moving them.

Grateful for any queries or recipes. Many thanks! I’d even write it up as a #howto topic if it’s good. :slightly_smiling:


How merge categories?
(Régis Hanol) #2

If you want to move all the topics from one category to another

Topic.where(category_id: <from_category_id>).update_all(category_id: <to_category_id>)

where “<from_category_id>” is the id of the origin category and “<to_category_id>” is the destination category.

If you want to move some topics from one category to another

topic_ids = [<topic_id_1>, <topic_id_2>, ...]
Topic.where(id: topic_ids).update_all(category_id: <to_category_id>)

where “<topic_id_1>, <topic_id_2>, ...” is a list of the topic ids you want to move.

If you execute either of these, category stats will be wrong but should fix themselves after a day.


Bulk change categories without logging edit
Force recount of topics in category after moving them
Moved topics to new category via ruby console, topic counts wrong on old category
Bulk selecting larg number of topics crashes browser
(Tobias Eigen) #3

thanks, @zogstrip! Much appreciated. What about tagging the topics first? Can you provide the query for that?

Update: I ended up doing the bulk move using the command you suggest, which appears to have worked perfectly. W00t! Thank you!

A little digging this morning revealed that @riking had also suggested a similar rails command in another topic:

There is also a topic I found for ‘converting categories to tags’. That would be a great place to share commands for bulk adding tags if it’s possible, as suggested by @erlend_sh.


(Tobias Eigen) #4

Whoa, this discovery solved my problem.

I’m golden. I can make bulk changes to tags and categories via discourse UI without bumping the topic. :thumbsup:


Any way to prevent notifications for post merges, moves?
(Tobias Eigen) #5

@zogstrip a downside to the query method of bulk moving topics into new categories is this:

Can’t delete this category because topic count is 200

I am trying to delete the old category (4 days later) but it won’t let me because of this message. Can you help me fix this?

Also, this method also had me move the “about this category” topic which also breaks some functionality.

So… conclusion is that using the bulk edit tags via the discourse UI is safer. :slight_smile:


(Régis Hanol) #6

This should solve your count problem

Category.where(id: <category_id>).update_all(topic_count: Topic.where(category_id: <category_id>).count)

(Tobias Eigen) #7

awesome - many, many thanks. that worked. I am so glad to have this bit of housekeeping taken care of.

Here, have a cute panda playing in the snow.


(Sam Saffron) #8

Panda…monium :panda_face:


#9

For anyone else who comes across this thread and doesn’t understand how to execute them, see the post below. It is linked to above also, but it’s not immediately obvious from the snippet that it shows exactly what to do, so click through and it will be clear.