问题描述
|
假设我有这些模型:
Physician
通过其Appointments
有许多many1ѭ。
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients,:through => :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians,:through => :appointments
end
我想编写一个范围或类似的内容,以便可以找到已确认任命的给定医师的所有患者。
最惯用的方法是什么?
解决方法
使用has_many:through关联来做到这一点:
has_many :confirmed_patients,:through => :appointments,:source => :patient,:class_name => \'Patient\',:conditions => { :confirmed => true }