开发者

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

开发者 https://www.devze.com 2023-03-13 11:58 出处:网络
Suppose I have these models: a Physician has many Patients through their Appointments. class Physician < ActiveRecord::Base

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

精彩评论

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