[英]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.