据我所知,当定义:counter_cache选项时,应在包含belongs_to声明的模型上指定。因此,当使用has_many through关联时,我有些不确定该如何处理(因为我认为在这种情况下不使用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
我希望使用:counter_cache选项来使查找属于医生的患者数量更加高效。
myPhysician.patients.count
提醒一下:Rails 3.1
干杯