【发布时间】:2017-06-29 20:06:26
【问题描述】:
我有模型,医生,有_many 患者。尝试编写一个查询,该查询只导致所有治愈患者的医生。因此,对于一个 Physician 的每个 Patient 都必须具有属性“is_cured: true”(或者至少对于所有 Physicians 患者来说不是 nil)。
目前为止有这个,但是当只有一名治愈患者时,医生会出现,而不是全部:
@physicians = Physician.includes(:patients)
.where.not(patients: { id: nil })
.where(patients: { is_cured: true })
型号:
class Physician < ApplicationRecord
has_many :patients
has_many :recommendations, through: :patients
end
class Patient < ApplicationRecord
belongs_to :physician
belongs_to :recommendation
end
class Recommendation < ApplicationRecord
has_many :patients
has_many :physicians, through: :patients
end
感谢您提供的任何帮助!
【问题讨论】:
标签: ruby-on-rails ruby postgresql