如何撰写ActiveRecord
查询以获得共享patients
的{{1}}个数(appointment
可以有多个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
那样得到它。但是真的需要写一个干净的activerecord查询以及hospital_id不为null的地方
由于
答案 0 :(得分:1)
据我所知,您希望在某些条件下执行多个嵌套连接。这就是干净利落的方式: -
get_result()