# Eager loading by sql

**URL:** <https://www.ruby-forum.com/t/eager-loading-by-sql/149660>\
**Category:** Rails\
**Created:** [September 27, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/eager-loading-by-sql/149660 "2008-09-27T15:28:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2008, 3:28pm UTC](https://www.ruby-forum.com/t/eager-loading-by-sql/149660/1 "2008-09-27T15:28:09Z")

</div>

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 S.

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2008, 5:18pm UTC](https://www.ruby-forum.com/t/eager-loading-by-sql/149660/2 "2008-09-27T17:18:23Z")

</div>

On 27 Sep 2008, at 14:28, John S.

> 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

---

<div class="post-metadata">

**Author:** ![captaffy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/f04885/32.png) [@captaffy](https://www.ruby-forum.com/u/captaffy)\
**Post date:** [September 27, 2008, 9:10pm UTC](https://www.ruby-forum.com/t/eager-loading-by-sql/149660/3 "2008-09-27T21:10:56Z")

</div>

Frederick C. 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 S.
