使用 column_names 时,有没有办法减少 counter_culture gem?

Is there a way to decrease in counter_culture gem when using column_names?

我需要减少组中活跃用户的计数器,实际上当组中的新用户从不活跃变为活跃时,计数器正在正确增加。但是当从活动状态变为非活动状态然后再次变为活动状态时,活动用户的计数器会增加一个。

所以我需要的是在从活动状态变为非活动状态时减少计数器。

直到现在我尝试使用 delta_magnitude 参数来减少它但没有用。

这就是 class 实际上

的样子
class GroupUser < ApplicationRecord
    attr_accessor :send_challenge

    belongs_to :group
    belongs_to :user

    counter_culture :group, column_name: proc { |model| model.active? ? 'users_count' : nil },
                                   column_names: {
                                     ['group_users.active = ?', true] => 'users_count'
                                   }

如您所见,我使用 dynamic-column-names

这是我目前所做的尝试

class GroupUser < ApplicationRecord
    attr_accessor :send_challenge

    belongs_to :group
    belongs_to :user

    counter_culture :group, column_name: proc { |model| model.active? ? 'users_count' : nil },
                                   column_names: {
                                     ['group_users.active = ?', true] => 'users_count'
                                   },
                                   delta_magnitude: 1

我在这里添加了 delta-magnitude 试图让它减少但没有奏效。

我有办法表达一个条件让它减少吗?

最后我只是在从活动变为非活动的同一查询中手动减少它。如果有人知道正确答案,下次会有用。

如果您使用包含回调的正常保存更新活动属性,当前 counter_culture 版本实际上会重新计算值更改的计数。 根据自述文件:

Updates counter cache when values change, not just when creating and destroying

在报告时这可能是一个错误,但现在应该可以了。