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