2016-10-03 3 views
1

私はまだかなり新しいレールで、これを理解しようとしています。フィルタアクティブレコードで別のアクティブレコード

私はモデルのカップルがあります:私はエリア内の位置に基づいて、いくつかのマップマーカーをフィルタリングしていますvendor_locations

経由地をHAS_MANYベンダー ベンダーbelongs_toの

予定を=:

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}) 

をしかし、働いていない、明らかにthatsの:私はアクティブなレコードを設定しようとしていた

が含まれています。 @vendor_locationsに基づいてどのようにフィルタリングすることができますか?

+0

は今働いて定義します。何かエラーが出ますか? – Pavan

答えて

0

Pluckのは、予定をフィルタリングするvendor_locationsからVENDOR_ID:

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

それはそれをしました!ありがとう! –

関連する問題