title: How to eager load objects with a custom join in rails?
body: <h3>Background</h3>
  
  <p>Normal rails eager-loading of collections works like this:</p>
  
  <pre><code>Person.find(:all, :include=&gt;:companies)
  </code></pre>
  
  <p>This generates some sql which does</p>
  
  <pre><code>LEFT OUTER JOIN companies ON people.company_id = companies.id
  </code></pre>
  
  <h3>Question</h3>
  
  <p>However, I need a custom join (this could also arise if I was using <code>find_by_sql</code>) so I can't use the vanilla <code>:include =&gt; :companies</code></p>
  
  <p>The custom join/sql will get me all the data I need, but how can I tell activerecord that it belongs to the associated <code>Company</code> objects rather than just being a pile of extra rows?</p>
  
  <h3>Update</h3>
  
  <p>I need to put additional conditions in the join. Something like this:</p>
  
  <pre><code>SELECT blah blah blah
  LEFT OUTER JOIN companies ON people.company_id = companies.id AND people.magical_flag IS NULL 
  &lt;Several other joins&gt;
  WHERE blahblahblah
  </code></pre>