【发布时间】:2014-07-08 22:27:53
【问题描述】:
我之前在我的应用中设置了一些 counter_caches,但仅用于简单的 belongs_to 关系。
我正在做很多类似的查询
user.collections.got.count
where got 是我的集合模型中的一个范围
belongs_to :user
scope :got, -> { where(status: 'Got') }
我可以设置一个 counter_cache 来计算标记为“got”的 user.collections 的数量吗? 我看到的问题是 counter_cache 仅使用 create 或 destroy 进行更新,而不是 update 操作。有什么好的方法吗?
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 counter-cache