2011-06-14 46 views
0

假設我有這些模型:Physician有許多PatientsAppointments如何在連接模型中編寫「範圍」?

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 

我想寫一個範圍或類似的東西,以便我可以找到一個給定的醫師的任命的患者被確認。

什麼是最習慣的方式來做到這一點?

回答

1

要使用的has_many做到這一點:通過關聯:

has_many :confirmed_patients, :through => :appointments, :source => :patient, :class_name => 'Patient', :conditions => { :confirmed => true }