开发者

How to write a "scope" on a join model?

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 }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜