If I want to rebuild the reaction count and summary, is there a way to do it?
Like:bundle exec rake reactions:recount
In a Support discussion titled How do you rebuild the post or reaction count, SubStrider asked if there’s a way to recount reaction counts and summaries, referencing a potential rake task. JammyDodger suggested that the jobs::EnsureDbConsistency background job might handle this and advised manually triggering it via /sidekiq/scheduler — while also asking for context on why the recount was needed. SubStrider clarified they were testing an import of reactions from XenForo to Discourse, where data imported correctly but didn’t display properly on posts. They resolved the issue by running a custom Rails runner script that recalculates reaction_users_count for each reaction, then updates the discourse_reactions_reactions custom field on affected posts to reflect the correct counts and metadata. A screenshot was included to show the successful result. The solution involved direct database manipulation via Rails console, ensuring post-level reaction summaries were properly rebuilt after import.
If I want to rebuild the reaction count and summary, is there a way to do it?
Like:bundle exec rake reactions:recount
Would this be covered by the jobs::EnsureDbConsistency background job?
You can manually trigger it from your /sidekiq/scheduler page if you want to speed it up and see if that rebuilds your counts for you.
Though if you can go into a little more detail about why you need to recalculate them it may add some extra context that could be important.
Thank you for your response. I was doing the test import of reactions from Xenforo to Discourse. The data was correctly imported but not showing up on posts with username and all.
Have managed to do it now.
Used this script
RAILS_ENV=production bundle exec rails runner "
counts = DiscourseReactions::ReactionUser.group(:reaction_id).count
DiscourseReactions::Reaction.where(id: counts.keys).find_each do |reaction|
reaction.update_column(:reaction_users_count, counts[reaction.id])
end
posts_to_update = Post.where(id: DiscourseReactions::Reaction.select(:post_id).distinct)
posts_to_update.find_each do |post|
post_reactions = post.reactions.where('reaction_users_count > 0')
custom_field_value = post_reactions.map do |reaction|
{
id: reaction.reaction_value,
type: reaction.reaction_type,
count: reaction.reaction_users_count
}
end
if custom_field_value.present?
post.custom_fields['discourse_reactions_reactions'] = custom_field_value.to_json
else
post.custom_fields.delete('discourse_reactions_reactions')
end
post.save_custom_fields(true)
end
"