title: When used with a paginated query, how many joined records do includes load into memory?
body: <p>One particular page in a Rails app keeps raising <code>Error R15 (Memory quota vastly exceeded)</code> errors on Heroku. </p>
  
  <p>This page has several remote <a href="https://datatables.net" rel="nofollow noreferrer">datatables</a>. These datatables are populated by ajax queries that look something like this. </p>
  
  <pre><code>User.
    find( params[:id] ).
    events.
    includes(:city,:state,:country).
    where("users.name ilike :search or cities.name ilike :search or states.name ilike :search or countries.name ilike :search", search: "%#{params[:search][:value]}%" ).
    page(page).per(10)
  </code></pre>
  
  <p>The tables are large, but as the query is paginated, I did not expect much loaded into memory. It was suggested to me that while paginate may limit how many <code>Events</code> are loaded into memory, the <code>includes</code> line will load all the <code>city</code>, <code>state</code> and <code>county</code> records. </p>
  
  <ol>
  <li>Does this sound correct? Will the above query load all joined tables
  referenced through the <code>includes</code> call?</li>
  <li>How should this query be changed to ensure memory is used efficiently?</li>
  </ol>