【发布时间】:2013-08-05 03:33:07
【问题描述】:
我正在学习 Rails,但遇到了一个问题。我的模型具有多对多的关系。很难解释我正在使用的模型,因为它们对我的工作领域来说太具体了,所以我将使用 rails guide 中的示例并稍作修改。
这是模型:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
attr_accessor :appointment_time
belongs_to :physician
belongs_to :patient
end
class Patient < ActiveRecord::Base
attr_accessor :name, :age
has_many :appointments
has_many :physicians, through: :appointments
end
我想要一份带有 Appointment.appointment_time 的患者列表。
我可以使用physician.patients 列出与医生相关的所有患者。我怎样才能包括预约时间?一旦我有了患者名单,我就可以考虑查询约会,但我想知道是否有一种“rails”方式来做到这一点(类似于physician.patients_with_appointments)。如果没有,那么有效的方法是什么?
【问题讨论】:
-
你为什么不想使用'physician.patients.include(:appointments)'?
-
请注意,您可能无法得到适用于您的特定代码的答案,因为您选择使用假代码示例。
标签: ruby-on-rails model associations