Rails:在has_many中获取“直通”的最佳方式:通过

时间:2014-01-26 15:28:24

标签: ruby-on-rails ruby activerecord

假设我们有来自Rails指南的着名场景:

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

在给定一名医生和一名患者的情况下,加载 约会实例的最佳方法是什么? 我一直在做Appointment.where(physician: my_physician, patient: my_patient,但感觉很脏。有没有一种方法可以通过:record?

返回单个单一的方法

1 个答案:

答案 0 :(得分:0)

您可以使用

my_physician.appointments.where(patient: my_patient)