我对rails还是个新手,正在努力弄清楚这个问题。
我有几个模型:
appointments = belongs_to供应商vendor通过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
我正在尝试过滤与搜索区域中的供应商相关联的活动appointment。
我试图用include设置活动记录:
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)