title: Why does the where() method run SQL queries after all nested relations are eager-loaded?
body: <p>In my controller method for the the index view I have the following line.</p>
  
  <pre><code>@students_instance = Student.includes(:memo_tests =&gt; {:memo_target =&gt; :memo_level})
  </code></pre>
  
  <p>So for each <code>Student</code> I eager-load all necessary info.</p>
  
  <p>Later on in a <code>.map</code> block, I call the <code>.where()</code> method on one of the relations as shown below.</p>
  
  <pre><code>@all_students = @students_instance.map do |student|
  
  ...
  
  last_pass = student.memo_tests.where(:result =&gt; true).last.created_at.utc
  difference_in_weeks = ((last_pass.to_i - current_date.to_i) / 1.week).round
  
  ...
  
  end
  </code></pre>
  
  <p><strong>This leads to a single SQL query for each <code>student</code>.</strong> And since I have over 300+ students, leads to very slow load times and over 300+ SQL queries.</p>
  
  <p>Am I right in thinking that this is caused by the <code>.where()</code> method. I think this because I have checked everything else and these are the two lines that cause all of the queries.</p>
  
  <p><strong>More importantly, is there a better way to do this that reduces these queries to a single query?</strong></p>