Eager loading by sql

I can see how to do eager loading of associations in the cases where you're letting AR construct the SQL and passing in :include =>{:assoc1,:assoc2} and so on. But my case using LEFT OUTER JOIN it generates is mind numbingly slow so I have to hand code my SQL to get an acceptable speed. But doing this means that I can't see how to get the thing to eager load.

The issue is that I have and M2M relationship. model_1 and model_2 both have a mutual HABTM relationship. When I select all instances of model_1 that are linked to a particular instance of model_2 I also want to see for each instance of model_1 all the instances of model_2 that it's linked to. I can see how to generate such a query by hand coding, and the result set will be something like

model_1.id,model_1.field_1, model_1.field_2, model_2.id,model_2.field_1 1 avalue anothervalue 20 m2_value 1 avalue anothervalue 650 m2_different_value 1 avalue anothervalue 785 m2_another_different_value

and so on. Now that's the kind of result set that AR must be getting back when it automagically creates SQL for eager loading. So how does it work out to creating 3 instances of model_2 linked to one instance of model_1 from the above result set?

To eager load the associations given the above result set does AR need to know that the input parameters had an :include in them. Or does it work things out purely from the result set?

If it can work purely from a result set then I'll be able to hand code the SQL. If I have to tap on :include =>{:something} into automagic SQL creation then I'm stuffed.

Any thoughts?

John Small

On 27 Sep 2008, at 14:28, John Small

To eager load the associations given the above result set does AR need to know that the input parameters had an :include in them. Or does it work things out purely from the result set

It's not by inspection of the result set (although it does used a
certain well defined set of aliased column names. There's a plugin
called eager finder SQL that claims to allow eager loading with custom
SQL.

Fred

Frederick Cheung wrote:

It's not by inspection of the result set (although it does used a certain well defined set of aliased column names. There's a plugin called eager finder SQL that claims to allow eager loading with custom SQL.

Fred

Perfect, just what I'm looking for.

Thanks

John Small