Force recount of topics in category after moving them

AI-generated summary

Users are discussing an issue with category topic counts not updating after moving topics between categories. omarfilip reports that counts remain incorrect even after 1.5 days and manual triggering of the CategoryStats job. fefrei suggests a Ruby script to manually recalculate counts, which they and jerdog confirm works, though it didn’t resolve the issue for omarfilip or codinghorror. neil confirms the count update runs daily and advises checking the Sidekiq scheduler, but manual runs don’t always fix the problem. The community agrees that automatic recalculation would be ideal, similar to post count updates.

Can the topic count for a category be forced to update after the topics have been moved to a different category?

I used this method to move topics around after merging two forums, but the counts are still off after 1.5 days.

https://meta.discourse.org/t/bulk-move-many-topics-from-one-category-to-another/38704/2

Shouldn’t the category counts rebuild after a day @neil?

It would be nice if they did! However, that didn’t work out for us when we did a similar move.

The following Ruby code, executed in the Rails console, will either fix the issue or set your database on fire :fire:, so backup accordingly :wink:

Category.find_each { |c|
    c.topic_count = Topic.where(category: c).count - 1 # -1 for about post
    c.save!
}

(I’d still love for these to be re-counted occasionally, just as I’d do for post counts :slight_smile:)

Yes this is happenings once a day, doing this to update the counts:

Seems to work for me.

@fefrei @omarfilip Please go to sidekiq scheduler (/sidekiq/scheduler) and see what the last run of CategoryStats shows.

I’ve triggered mine manually yesterday so the scheduler won’t mean much right now:

The manual triggering of CategoryStats did not change the counts, though.

I ran Felix’s ruby code earlier today and that dropped the topic counts in the empty categories. It did not increase the topic count elsewhere like I thought it would.

I had also triggered that job manually, as far as I recall. It completed successfully, but had no effect.

Mine also ran but didn’t change the counts. I also have one which is -1 as well.

image

I will run the code from @fefrei and see if that makes a difference.

Edit: running the code worked in the rails console