# Extending/Constraining Associations when Eager Loading?

**URL:** <https://www.ruby-forum.com/t/extending-constraining-associations-when-eager-loading/85115>\
**Category:** Rails\
**Created:** [February 10, 2007, 3:32am UTC](https://www.ruby-forum.com/t/extending-constraining-associations-when-eager-loading/85115 "2007-02-10T03:32:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Webmanager](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc79bd/32.png) [@Webmanager](https://www.ruby-forum.com/u/Webmanager)\
**Post date:** [February 10, 2007, 3:32am UTC](https://www.ruby-forum.com/t/extending-constraining-associations-when-eager-loading/85115/1 "2007-02-10T03:32:15Z")

</div>

Hi,

This example –  
[http://blog.hasmanythrough.com/2006/03/01/association-goodness-2](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!

---

<div class="post-metadata">

**Author:** ![Webmanager](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/bc79bd/32.png) [@Webmanager](https://www.ruby-forum.com/u/Webmanager)\
**Post date:** [February 10, 2007, 8:07am UTC](https://www.ruby-forum.com/t/extending-constraining-associations-when-eager-loading/85115/2 "2007-02-10T08:07:17Z")

</div>

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.”  
([ActiveRecord::Associations::ClassMethods](http://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html))

Cheers.

> Hi,
> 
> This example –  
> [has\_many :through - New association goodness in Rails 1.1, part 2](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!
