【问题标题】:Does counter cache respect counter_sql or conditions?计数器缓存是否尊重 counter_sql 或条件?
【发布时间】:2013-01-14 06:00:32
【问题描述】:

在 Rails 3.2 中,关联选项 counter_cache 是否链接到其他选项,例如 counter_sqlconditions

例如,如果我有这样的关联:

class User
  has_many :items, :through => :orders
  has_many :active_items, :through => :orders
           :conditions => 'active = true', :source => :item

class Order
  belongs_to :users, :counter_cache => :active_items_count
  belongs_to :items

class Item
  has_many :users, :through => :orders

计数器缓存会遵守 active 条件吗? (意味着不活动的项目不会被计算在内) 那么使用finder_sql/counter_sql

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 associations counter-cache


    【解决方案1】:

    由于您提到的原因,我最终很少使用计数器缓存,而是始终创建自己的。对 on_save 和 on_create 回调做出反应并调用一个方法来更新计数。

    如果您确实创建了自己的一个提示,请使用@instance.update_column(:column, value)。这将确保不会递归调用回调。

    【讨论】:

      猜你喜欢
      • 2019-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-05
      • 2014-06-03
      • 2017-01-10
      • 2013-10-01
      相关资源
      最近更新 更多