Eager loading associations fix belongs_to doesn't work

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?

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.

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?

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.

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.