Forum: Ruby on Rails Extending/Constraining Associations when Eager Loading?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Webmanager (Guest)
on 2007-02-10 04:32
Hi,

This example --
http://blog.hasmanythrough.com/2006/03/01/associat...
-- 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!
Webmanager (Guest)
on 2007-02-10 09:07
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/As...)

Cheers.

> Hi,
>
> This example --
> http://blog.hasmanythrough.com/2006/03/01/associat...
> -- 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!
This topic is locked and can not be replied to.