【发布时间】:2014-04-16 01:13:34
【问题描述】:
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
scope :physicals, -> { where appointment_type: 'physical' }
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
end
如何在一个查询中为医生访问带有体检的患者列表?反过来(有不同类型的不同约会的患者)?然后我可以用physician.patients_with_physicals = [patient] 之类的东西来设置它吗?
【问题讨论】:
-
首先,不是
HABTM,而是has_many =>through。
标签: sql ruby-on-rails activerecord scope has-and-belongs-to-many