# Eager loading associations fix belongs\_to doesn't work

**URL:** <https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040>\
**Category:** Rails\
**Created:** [April 15, 2007, 5:30am UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040 "2007-04-15T05:30:04Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 15, 2007, 5:30am UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040/1 "2007-04-15T05:30:04Z")

</div>

Hi,

I tried using the eager\_custom\_sql.rb fix to be able to eager load  
associations using find\_by\_sql.

I was able to get this working for my has\_many associations, however, Im  
having trouble getting it to work for belongs\_to associations.

Has anyone experienced this? Any fixes?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 3:56pm UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040/2 "2007-04-16T15:56:50Z")

</div>

Aryk G. wrote:

> I tried using the eager\_custom\_sql.rb fix to be able to eager load  
> associations using find\_by\_sql.
> 
> I was able to get this working for my has\_many associations, however, Im  
> having trouble getting it to work for belongs\_to associations.
> 
> Has anyone experienced this? Any fixes?

belongs\_to doesn’t support a :finder\_sql option, so you can’t  
use fully-custom sql. I’d be interested to see a belongs\_to  
that actually requires it.

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

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 16, 2007, 6:51pm UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040/3 "2007-04-16T18:51:20Z")

</div>

Hmm, so if you want to use manual sql, how can you return a belongs\_to  
object alongside the main object or do you just have to use the standard  
“find” with the “include” option?

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 17, 2007, 3:48pm UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040/4 "2007-04-17T15:48:26Z")

</div>

Aryk G. wrote:

> Hmm, so if you want to use manual sql, how can you return a belongs\_to  
> object alongside the main object or do you just have to use the standard  
> “find” with the “include” option?

Oh, you mean the belongs\_to is the association you want eager loaded,  
not that you want to use the :include method in the declaration  
of a belongs\_to, eager loading its descendants?

If so, find\_by\_sql and the :include option should work if you’ve  
correctly written all the custom sql joins and field aliasing.

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

---

<div class="post-metadata">

**Author:** ![ericgoodwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/c68b51/32.png) [@ericgoodwin](https://www.ruby-forum.com/u/ericgoodwin)\
**Post date:** [April 17, 2007, 10:46pm UTC](https://www.ruby-forum.com/t/eager-loading-associations-fix-belongs-to-doesnt-work/92040/5 "2007-04-17T22:46:02Z")

</div>

Yeah, this is what Im attempting to do. I have the eager\_custom\_sql.rb  
fix. In the patch, they write:

# This Active Record mod enables eager loading of associations

# (the :include option) for both the find\_by\_sql method and has\_many

# and has\_and\_belongs\_to\_many associations that use the :finder\_sql

option.

So they don’t include the “belongs\_to” case. Nevertheless, I tried  
formatting it as you said and I got it working with the “has\_many”  
relationships, but not with my belongs\_to assocations.

Anyone had any success getting find\_by\_sql working with including a  
belongs\_to association?

Mark Reginald J. wrote:

> Aryk G. wrote:
> 
> > Hmm, so if you want to use manual sql, how can you return a belongs\_to  
> > object alongside the main object or do you just have to use the standard  
> > “find” with the “include” option?
> 
> Oh, you mean the belongs\_to is the association you want eager loaded,  
> not that you want to use the :include method in the declaration  
> of a belongs\_to, eager loading its descendants?
> 
> If so, find\_by\_sql and the :include option should work if you’ve  
> correctly written all the custom sql joins and field aliasing.
> 
> –  
> We develop, watch us RoR, in numbers too big to ignore.
