活动记录查询以获取rails中嵌套关联的计数

时间:2016-07-14 19:30:43

标签: ruby-on-rails ruby-on-rails-4 activerecord ruby-on-rails-3.2

如何撰写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的地方

由于

1 个答案:

答案 0 :(得分:1)

据我所知,您希望在某些条件下执行多个嵌套连接。这就是干净利落的方式: -

get_result()