# 如何重新构建帖子或反应计数

**URL:** <https://meta.discourse.org/t/how-do-you-rebuild-the-post-or-reaction-count/369504>\
**Category:** Support\
**Created:** [2025年六月10日 04:43 UTC](https://meta.discourse.org/t/how-do-you-rebuild-the-post-or-reaction-count/369504 "2025-06-10T04:43:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SubStrider](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/substrider/32/512604_2.png) [@SubStrider](https://meta.discourse.org/u/SubStrider)\
**Post date:** [2025年六月10日 04:43 UTC](https://meta.discourse.org/t/how-do-you-rebuild-the-post-or-reaction-count/369504/1 "2025-06-10T04:43:49Z")

</div>

如果我想重建反应计数和摘要，有没有办法做到？

例如：bundle exec rake reactions:recount

---

<div class="post-metadata">

**Author:** ![JammyDodger](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/jammydodger/32/254611_2.png) [@JammyDodger](https://meta.discourse.org/u/JammyDodger)\
**Post date:** [2025年六月10日 06:36 UTC](https://meta.discourse.org/t/how-do-you-rebuild-the-post-or-reaction-count/369504/2 "2025-06-10T06:36:51Z")

</div>

这是否属于 `jobs::EnsureDbConsistency` 后台作业的覆盖范围？

如果您想加快速度并查看它是否为您重建计数，您可以从您的 `/sidekiq/scheduler` 页面手动触发它。

虽然如果您能详细说明为什么需要重新计算它们，这可能会增加一些可能很重要的额外背景信息。

---

<div class="post-metadata">

**Author:** ![SubStrider](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/substrider/32/512604_2.png) [@SubStrider](https://meta.discourse.org/u/SubStrider)\
**Post date:** [2025年六月10日 07:32 UTC](https://meta.discourse.org/t/how-do-you-rebuild-the-post-or-reaction-count/369504/3 "2025-06-10T07:32:34Z")

</div>

感谢您的回复。我正在进行从 XenForo 到 Discourse 的反应测试导入。数据已正确导入，但未在带有用户名等的帖子中显示。

现在已经解决了。

 ![Screenshot 2025-06-10 at 12.58.45 PM](https://global.discourse-cdn.com/meta/original/4X/0/7/2/0723c2a4f59d9ea9f0fa91f43a87cb4e0458ba73.png)

使用了这个脚本

```plaintext
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

\"

```
