[英]Filter Active Record On Another Active Record
我對rails很陌生,並試圖解決這個問題。
我有幾個型號:
約會= belongs_to供應商供應商has_many位置通過vendor_locations
我根據區域中的位置過濾一些地圖標記:
if params[:search].present?
location_ids = Location.near(params[:search], 50, order: '').pluck(:id)
@vendor_locations = VendorLocation.includes(:location).where(location_id: location_ids)
else
location_ids = Location.near([session[:latitude], session[:longitude]], 50, order: '').pluck(:id)
@vendor_locations = VendorLocation.includes(:location).where(location_id: location_ids)
end
我正在嘗試過濾那些與搜索區域中的供應商相關聯的活動APPOINTMENTS。
我試圖用以下內容設置活動記錄:
def index
if params[:service].blank?
@appointments = Appointment.includes(:vendor).where(vendors: {id: @vendor_locations.vendor_id})
但那顯然不起作用。 關於如何基於@vendor_locations進行過濾的任何建議?
從vendor_locations中抽取vendor_id來過濾約會:
@vendor_locations = VendorLocation.where(location_id: location_ids)
@appointments = Appointment.
includes(:vendor).
where vendor_id: @vendor_locations.select(:vendor_id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.