0

have_many イベントを持つアクティビティがあります。イベントは、アクティビティが発生する日時です。

アクティビティ インデックスに、今後少なくとも 1 つのイベントがあるすべてのアクティビティをリストしたいと考えています。

ransack を使用して、他のフィルタリングを行ったり、カテゴリとして吸ったりしています。

リレーション全体で大なり条件をフィルタリングするにはどうすればよいですか?

私はできると思った:

obj = search
        .joins(:event)
        .where(:events => ['end_at > ?', Date.today ])  

しかし、私はエラーが発生します:

undefined method `join' for #<Ransack::Search:0x007f8aa8eff918>

更新 して、より多くのコンテキストを提供するために、私のコントローラーは次のようになります

def index            
    if (params[:oldestchild].present? && params[:oldestchild].to_i > 0) ||   
      (params[:youngestchild].present? && params[:youngestchild].to_i > 0)
      @search = Activity
       .where(' (oldest > ?z and youngest < ?) or (oldest > ? and youngest < ?) ', 
         params[:oldestchild], params[:oldestchild],
         params[:youngestchild], params[:youngestchild], 
         )
       .search(params[:q])
    else
       @search = Activity
         .search(params[:q])
    end  

    @activities = Activity.filter(params, @search, request).includes(:provider).includes(:location)

    if current_parent and current_parent.children.first
      @min_child_age = current_parent.min_child_age 
      @max_child_age = current_parent.max_child_age
    end
end

私のモデルにはこのフィルターメソッドがあります:

  def self.filter(params, search, request)          
        obj = search.result(:distinct => true)      
#       obj = search
#               .joins(:event)
#               .where(:events => ['end_at > ?', Date.today ])      

    if params[:within].present? && (params[:within].to_i > 0)
          obj = obj.where(:location_id => self.build_locations_array(request))
        end                         
        if  params[:q].present? and params[:q][:category_ids].present? 
            obj = obj
            .joins(:categories)
            .where('categories.id' => params[:q][:category_ids])                                                                        
        end
        if params[:date_range] == "This month"              
            obj = obj.where(:start => (Date.today)..(Date.today.at_end_of_month))   
        end

        if params[:date_range] == "Next month"  
            date = Date.today + 1.month             
            obj = obj.where(:start => (date.at_beginning_of_month)..(date.at_end_of_month))                                                                     
        end
        if params[:date_range] == "Next 3 Months"               
            date = Date.today + 3.month             
            obj = obj.where(:start => (Date.today)..(date.at_end_of_month))                                                                     
        end
        if params[:date_range] == "Whenever"             
            obj = obj.where("start > ?",  Date.today)                                                                       
        end 

        obj.paginate(:page => params[:page],  :per_page => 5)               
  end

更新

機能しているがかなり醜いオプションの1つは、SQLを使用することです。

obj = search.result(:distinct => true)
 .where("exists (select events.id from events 
          where events.activity_id = activities.id 
          and end_at > ?)", Date.today)
4

1 に答える 1