Extending/Constraining Associations when Eager Loading?


#1

Hi,

This example –
http://blog.hasmanythrough.com/2006/03/01/association-goodness-2
– shows how to pass a parameter to an extended association; Ie: books =
josh.books.by_role(“author”)

Is there ANY way to do this with a dynamic variable in combination with
active eager loading? Ie: This works: “p.fgp.by_fg(9)”, but this does
not: “p = Proposal.find(317, :include => :fgp.by_fg(9))”, giving
“NoMethodError: undefined method `by_fg’ for :fgp:Symbol” when used with
:include/eager loading.

All I’m after is having AR’s eager loading joins contrain the joined
tables at the join rather than the where clause. Ie: “LEFT OUTTER JOIN
fgp ON … AND fgp.id_fg = 9

I can’t find any way to accomplish this outside find_by_sql or :joins,
and I’d very much like to retain the resulting object composition AR
gives when using :include.

Thanks and best to everyone. Rails is great!

Cheers!


#2

To respond to my own post, this bit from the docs on eager loading
likely details the root of the issue (no?): “When eager loaded,
conditions are interpolated in the context of the model class, not the
model instance. Conditions are lazily interpolated before the actual
model exists.”
(http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)

Cheers.

Hi,

This example –
http://blog.hasmanythrough.com/2006/03/01/association-goodness-2
– shows how to pass a parameter to an extended association; Ie: books =
josh.books.by_role(“author”)

Is there ANY way to do this with a dynamic variable in combination with
active eager loading? Ie: This works: “p.fgp.by_fg(9)”, but this does
not: “p = Proposal.find(317, :include => :fgp.by_fg(9))”, giving
“NoMethodError: undefined method `by_fg’ for :fgp:Symbol” when used with
:include/eager loading.

All I’m after is having AR’s eager loading joins contrain the joined
tables at the join rather than the where clause. Ie: “LEFT OUTTER JOIN
fgp ON … AND fgp.id_fg = 9

I can’t find any way to accomplish this outside find_by_sql or :joins,
and I’d very much like to retain the resulting object composition AR
gives when using :include.

Thanks and best to everyone. Rails is great!

Cheers!