Note that there are some explanatory texts on larger screens.

plurals
  1. POSelect records where at least some has_many relations are in the future
    primarykey
    data
    text
    <p>I have activities which have_many events - events are the date and time the activities occur. </p> <p>In my activities index, I want to list all activities that have at least one event in the future. </p> <p>I'm using ransack to do my other filtering , suck as categories etc. </p> <p>How can i filter for a greater than condition accross a relation?</p> <p>I thought I would be able to do:</p> <pre><code>obj = search .joins(:event) .where(:events =&gt; ['end_at &gt; ?', Date.today ]) </code></pre> <p>but i get an error:</p> <pre><code>undefined method `join' for #&lt;Ransack::Search:0x007f8aa8eff918&gt; </code></pre> <p><strong>UPDATE</strong> so to give more context, my controler looks like this</p> <pre><code>def index if (params[:oldestchild].present? &amp;&amp; params[:oldestchild].to_i &gt; 0) || (params[:youngestchild].present? &amp;&amp; params[:youngestchild].to_i &gt; 0) @search = Activity .where(' (oldest &gt; ?z and youngest &lt; ?) or (oldest &gt; ? and youngest &lt; ?) ', 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 </code></pre> <p>and my model has this filter method:</p> <pre><code> def self.filter(params, search, request) obj = search.result(:distinct =&gt; true) # obj = search # .joins(:event) # .where(:events =&gt; ['end_at &gt; ?', Date.today ]) if params[:within].present? &amp;&amp; (params[:within].to_i &gt; 0) obj = obj.where(:location_id =&gt; self.build_locations_array(request)) end if params[:q].present? and params[:q][:category_ids].present? obj = obj .joins(:categories) .where('categories.id' =&gt; params[:q][:category_ids]) end if params[:date_range] == "This month" obj = obj.where(:start =&gt; (Date.today)..(Date.today.at_end_of_month)) end if params[:date_range] == "Next month" date = Date.today + 1.month obj = obj.where(:start =&gt; (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 =&gt; (Date.today)..(date.at_end_of_month)) end if params[:date_range] == "Whenever" obj = obj.where("start &gt; ?", Date.today) end obj.paginate(:page =&gt; params[:page], :per_page =&gt; 5) end </code></pre> <p>UPATE</p> <p>one option that is working, but is fairly ugly is to use sql. </p> <pre><code>obj = search.result(:distinct =&gt; true) .where("exists (select events.id from events where events.activity_id = activities.id and end_at &gt; ?)", Date.today) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload