在另一个活动记录上过滤活动记录

时间:2016-10-03 06:25:48

标签: ruby-on-rails activerecord controller

我对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进行过滤的任何建议?

1 个答案:

答案 0 :(得分:0)

从vendor_locations中抽取vendor_id来过滤约会:

@vendor_locations = VendorLocation.where(location_id: location_ids)
@appointments = Appointment.
                  includes(:vendor).
                  where vendor_id: @vendor_locations.select(:vendor_id)