# Eager loading : find\_with\_association

**URL:** <https://www.ruby-forum.com/t/eager-loading-find-with-association/82816>\
**Category:** Rails\
**Created:** [January 21, 2007, 11:54am UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816 "2007-01-21T11:54:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 11:54am UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/1 "2007-01-21T11:54:38Z")

</div>

in the script I am using, I checked the statement :

f = find\_with\_associations(options.merge(:sql =\> sanitized\_sql))

where  
sql = “SELECT…” my select, running fine in sql client mode or  
find\_by\_sql()  
and  
options.inspect  
=\> “{:include=\>[:user, :city, {:category=\>:domain}]}”

this find\_with\_associations() just bring me back an empty record

f  
=\> [#\<Proposal:0x3b5ae3c @attributes={“city\_id”=\>nil, “updated\_at”=\>nil,  
“title”=\>nil, “optel”=\>nil, “id”=\>nil, “show\_count”=\>nil,  
“description”=\>nil, “category\_id”=\>nil, “telephone”=\>nil,  
“user\_id”=\>nil, “valid\_until”=\>nil, “created\_at”=\>nil}\>]

what’s wrong with my nested include ???

tfyh

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 12:17pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/2 "2007-01-21T12:17:47Z")

</div>

Kad K. wrote:

> in the script I am using, I checked the statement :
> 
> f = find\_with\_associations(options.merge(:sql =\> sanitized\_sql))

That looks like my eager\_custom\_sql.rb mod.

> =\> [#\<Proposal:0x3b5ae3c @attributes={“city\_id”=\>nil, “updated\_at”=\>nil,  
> “title”=\>nil, “optel”=\>nil, “id”=\>nil, “show\_count”=\>nil,  
> “description”=\>nil, “category\_id”=\>nil, “telephone”=\>nil,  
> “user\_id”=\>nil, “valid\_until”=\>nil, “created\_at”=\>nil}\>]
> 
> what’s wrong with my nested include ???

Are you selecting your fields using the required tn\_rn aliasing?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 12:24pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/3 "2007-01-21T12:24:59Z")

</div>

Mark Reginald J. wrote:

> Kad K. wrote:
> 
> > in the script I am using, I checked the statement :
> > 
> > f = find\_with\_associations(options.merge(:sql =\> sanitized\_sql))
> 
> That looks like my eager\_custom\_sql.rb mod.
> 
> > =\> [#\<Proposal:0x3b5ae3c @attributes={“city\_id”=\>nil, “updated\_at”=\>nil,  
> > “title”=\>nil, “optel”=\>nil, “id”=\>nil, “show\_count”=\>nil,  
> > “description”=\>nil, “category\_id”=\>nil, “telephone”=\>nil,  
> > “user\_id”=\>nil, “valid\_until”=\>nil, “created\_at”=\>nil}\>]
> > 
> > what’s wrong with my nested include ???
> 
> Are you selecting your fields using the required tn\_rn aliasing?
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

the select is like this one :

SELECT _, (6366_acos(cos(0.853291)\*cos(cities.latitude) \*  
cos(cities.longitude - (0.040841))+sin(0.853291)_sin(cities.latitude)))  
as km FROM proposals INNER JOIN users ON proposals.user\_id = users.id  
INNER JOIN cities ON proposals.city\_id = villes.id INNER JOIN categories  
ON proposals.category\_id = categories.id INNER JOIN domains ON  
categories.domain\_id = domains.id WHERE proposals.city\_id IN  
(31556,37294,31547,37302,31548,31555,31546,31557,37236,31540,31541) AND  
(6366_acos(cos(0.853291)\*cos(cities.latitude) \*  
cos(cities.longitude-(0.040841))+sin(0.853291)\*sin(cities.latitude))) \<  
25.0 AND (valid\_until \>= ‘2007-01-21 09:25:15’ OR valid\_until IS NULL)  
ORDER BY km LIMIT 0,4

so, it’s a select all, only a calculated value as km…

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 12:42pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/4 "2007-01-21T12:42:50Z")

</div>

Kad K. wrote:

> (6366\*acos(cos(0.853291)\*cos(cities.latitude) \*  
> cos(cities.longitude-(0.040841))+sin(0.853291)\*sin(cities.latitude))) \<  
> 25.0 AND (valid\_until \>= ‘2007-01-21 09:25:15’ OR valid\_until IS NULL)  
> ORDER BY km LIMIT 0,4
> 
> so, it’s a select all, only a calculated value as km…

If you use the :include option you have to specially alias all your  
selected fields in the way described in the comment at the top of  
the mod, plus use left joins. And I’m not sure if it’s possible to  
also select some calculated fields.

Do you really need the returned proposal models to have the included  
associated models chained off them, or do you just need to join the  
other tables for use in the calculations and conditions?

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 12:53pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/5 "2007-01-21T12:53:50Z")

</div>

Mark Reginald J. wrote:

> Kad K. wrote:
> 
> > (6366\*acos(cos(0.853291)\*cos(cities.latitude) \*  
> > cos(cities.longitude-(0.040841))+sin(0.853291)\*sin(cities.latitude))) \<  
> > 25.0 AND (valid\_until \>= ‘2007-01-21 09:25:15’ OR valid\_until IS NULL)  
> > ORDER BY km LIMIT 0,4
> > 
> > so, it’s a select all, only a calculated value as km…
> 
> If you use the :include option you have to specially alias all your  
> selected fields in the way described in the comment at the top of  
> the mod, plus use left joins. And I’m not sure if it’s possible to  
> also select some calculated fields.
> 
> Do you really need the returned proposal models to have the included  
> associated models chained off them, or do you just need to join the  
> other tables for use in the calculations and conditions?
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

In this case I just need to join the other tables for use in the  
calculations and conditions,  
the resulting list is the proposals list… from there ones can link\_to  
the proposal.user only  
(:cities , :category and :domain models are used only for conditions and  
calculations…)

kad

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 1:00pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/6 "2007-01-21T13:00:52Z")

</div>

Kad K. wrote:

> In this case I just need to join the other tables for use in the  
> calculations and conditions,  
> the resulting list is the proposals list… from there ones can link\_to  
> the proposal.user only  
> (:cities , :category and :domain models are used only for conditions and  
> calculations…)

Don’t use the :include option then. Either use find\_by\_sql, or use a  
normal find with specified :select, :joins, :conditions, and :order  
options.

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [January 21, 2007, 3:12pm UTC](https://www.ruby-forum.com/t/eager-loading-find-with-association/82816/7 "2007-01-21T15:12:17Z")

</div>

Mark Reginald J. wrote:

> Kad K. wrote:
> 
> > In this case I just need to join the other tables for use in the  
> > calculations and conditions,  
> > the resulting list is the proposals list… from there ones can link\_to  
> > the proposal.user only  
> > (:cities , :category and :domain models are used only for conditions and  
> > calculations…)
> 
> Don’t use the :include option then. Either use find\_by\_sql, or use a  
> normal find with specified :select, :joins, :conditions, and :order  
> options.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.

thanks that’s what I did, running well…  
I’ve been in touch with Ilya Gregorik who wrote ’ Eager Find by SQL  
Pagination in Rails’ , he never tested w nested include…

is there any info somewhere about ‘when to use eager find… when not  
to use it’ … would be helpful for newbies understanding…
