【发布时间】:2016-07-14 19:30:43
【问题描述】:
如何编写ActiveRecord 查询以获取共享appointment 的patients 的数量(physician 可以有多个appointments)
class Hospital < ActiveRecord::Base
has_many :appointments
has_many :patients
has_many :physicians
end
class Physician < ActiveRecord::Base
has_many :appointments
has_many :patients, through: :appointments
belongs_to :hospital
end
class Appointment < ActiveRecord::Base
belongs_to :physician
belongs_to :patient
belongs_to :hospital
end
class Patient < ActiveRecord::Base
has_many :appointments
has_many :physicians, through: :appointments
belongs_to :hospital
end
我可以像Patient.first.hospital.appointments 一样得到它。但确实需要编写一个干净的 activerecord 查询,以及 hospital_id 不为空的地方
谢谢
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-4 activerecord ruby-on-rails-3.2