Suppose I have these models: a Physician
has many Patients
through their Appointments
.
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
I want to write a scope or something similar so that I can find all Patients of a given Physician whose Appointments are confirmed.
What is the most idiomatic 开发者_开发知识库way to do this?
To do this with a has_many :through association:
has_many :confirmed_patients, :through => :appointments, :source => :patient, :class_name => 'Patient', :conditions => { :confirmed => true }
精彩评论