title: Rails .joins() between tables and .where() condition
body: <h2>Summary</h2>
  
  <p>Thanks a lot for your help.
  I have a <code>locations</code> and <code>ads</code> table. Location <code>has_many :ads</code></p>
  
  <p>I would like to make a query of <code>Location</code> Model with a <code>join</code> with <code>Ad</code> Model, to filter entries on parameters of <code>Location</code> and <code>Ad</code>.</p>
  
  <pre><code>@locations = Location.joins(:ads).where(locations: location_params, ads: location_params[:ads_attributes])
  </code></pre>
  
  <p>This is the location_params method (empty fields will be deleted with another method that is working fine).</p>
  
  <pre><code>params.require(:location).permit(:country, {:ads_attributes =&gt; [:remote, :days]})
  </code></pre>
  
  <p>This is an example of my query. I have a method which remove empty fields from location_params. It works fine.</p>
  
  <pre><code>SELECT "locations".* FROM "locations" INNER JOIN "ads" 
  ON "ads"."location_id" = "locations"."id" 
  WHERE "ads_attributes"."remote" = $1  [["remote", "1"]]
  </code></pre>
  
  <p>In this case location_params has the following fields:</p>
  
  <pre><code>&lt;ActionController::Parameters {"ads_attributes"=&gt;
  &lt;ActionController::Parameters {"remote"=&gt;"1"} permitted: true&gt;} permitted: true&gt;
  </code></pre>
  
  <p>This is the result, an empty object even if I have entries with those parameters in my table</p>
  
  <pre><code>#&lt;Location::ActiveRecord_Relation:0x3fb83def8190&gt;
  </code></pre>
  
  <h2>Update</h2>
  
  <ol>
  <li>First Issue - Solved from <a href="https://stackoverflow.com/users/3941583/p%C3%A9ter-t%C3%B3th-toma">Péter Tóth</a></li>
  </ol>
  
  <p>Solution. Using <code>.includes(:ads)</code> for not re-executing the query with <code>@locations[0].ads</code>. </p>
  
  <pre><code>@locations = Location.joins(:ads).where(locations: {id: 1}, ads: {id: 1})
  </code></pre>
  
  <p>The problem is, when i select from location the <code>ads</code>, it will perform again the query and delete the previous filter ads.id = 1. </p>
  
  <pre><code>@locations[0].ads
  </code></pre>
  
  <p>The result is not only ad with id=1 will be selected but all ads of that @location[0] will be selected.</p>
  
  <ol>
  <li>Second Issue</li>
  </ol>
  
  <p>I am able to execute the query:</p>
  
  <pre><code>@locations = Location.joins(:ads).where(locations: {id: 1}, ads: {id: 1})
  </code></pre>
  
  <p>Or</p>
  
  <pre><code>@locations = Location.joins(:ads).where(location_params)
  </code></pre>
  
  <p>But not </p>
  
  <pre><code>@locations = Location.joins(:ads).where(locations: location_params, ads: ads_params)
  </code></pre>
  
  <p>But this can be solved as follows:
  Perform a first query on Location</p>
  
  <pre><code>@locations = Location.joins(:ads).where(@location_params.require(:location).permit(:id)).includes(:ads)
  </code></pre>
  
  <p>which returns all locations with those parameters, then I need to filter @locations based on the ads filters. The problem is that I am not able to do the following query.</p>
  
  <pre><code>@locations = @locations.joins(:ads).where(ads: @ads_params.require(:ads).permit(:id)).includes(:ads)
  </code></pre>