繁体   English   中英

Rails 4过滤器has_many通过

[英]Rails 4 filter has_many through

给定以下DoctorProfile模型,如何构造一个基于接受的保险过滤DoctorProfile的查询? 我意识到以后可以通过一个块来运行结果,但是我宁愿在数据库查询中执行此操作。

class DoctorProfile
  has_many :doctor_insurances
  has_many :accepted_insurances, -> { distinct }, through: :doctor_insurances, :source => :insurance_provider

class DoctorInsurance
  belongs_to :doctor_profile
  belongs_to :insurance_provider

现在,我有以下查询,并且我希望能够传递单个或多个InsuranceProvider实例,以过滤出其接受的保险属于保险提供商数组的医生

DoctorProfile.where(:specialty_id => 1) 
DoctorProfile.where(doctor_insurances: {accepted_insurances: [instance]}).references(doctor_insurances: [:accepted_insurances])

应该可以解决这个问题,传递尽可能多的实例,它将在查询中进行。

也许您可以尝试以下方法:

DoctorProfile.joins(:accepted_insurances).where(doctor_insurances: {id: [instance]})

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM