我的理解是,在定义:counter_cache选项时,它将在包含belongs_to声明的模型上指定.因此,当使用has_may通过关联时,我有点不确定如何处理这个问题(因为我认为在这种情况下不使用belongs_to声明):
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, :through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician, :counter_cache => appointment_count
end
class Patient < ActiveRecord::Base
end
Run Code Online (Sandbox Code Playgroud)
我希望使用:counter_cache选项来更有效地查找属于医生的患者数量.
myPhysician.patients.count
Run Code Online (Sandbox Code Playgroud)
仅供参考:Rails 3.1
干杯