# Help with extensions

**URL:** <https://www.ruby-forum.com/t/help-with-extensions/84026>\
**Category:** Rails\
**Created:** [January 31, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-extensions/84026 "2007-01-31T16:51:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [January 31, 2007, 4:51pm UTC](https://www.ruby-forum.com/t/help-with-extensions/84026/1 "2007-01-31T16:51:33Z")

</div>

I am working with extensions to has\_many associations, and I want to  
create  
a finder method that is conditional on the attributes of the object  
whose  
association I’m working with.

So, to use the example from the Agile Web D.: the authors show  
on  
pg 340 in the second edition how to pass an argument to a finder method.  
But  
I want to know if it’s possible to instead have the finder method look  
at  
attributes of the object on which the finder method is ultimately being  
executed (here, user) to determine the conditions.

class User

has\_many :reading  
has\_many :articles, :through =\> :readings do  
def rated\_at\_or\_above(rating) # this is the finder  
method in AWD – works fine  
find :all, :conditions =\> [‘rating \>= ?’, rating]  
end  
def rated\_at\_or\_above\_threshold # but can I do  
something  
like this? (doesn’t work)  
find :all, :conditions =\> [‘rating \>= ?’, user.threshold] #  
NoMethodError: undefined method user for Article:class  
end  
end  
end

How would I implement this? As the error suggests, the problem is that  
the  
article doesn’t know who its user is. So how can I pass that information  
to  
this method, given that it’s nested inside of this association?

Thanks.

(Btw, I tried to post this yesterday but it has not appeared, and  
another  
msg suggested that posts made from the Google G. page get lost  
sometimes  
– sorry if this is double-posted.)

---

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [January 31, 2007, 5:47pm UTC](https://www.ruby-forum.com/t/help-with-extensions/84026/2 "2007-01-31T17:47:09Z")

</div>

Andy E. wrote:

> class User
> 
> has\_many :reading  
> has\_many :articles, :through =\> :readings do  
> def rated\_at\_or\_above(rating) # this is the finder  
> method in AWD – works fine  
> find :all, :conditions =\> [‘rating \>= ?’, rating]  
> end  
> def rated\_at\_or\_above\_threshold # but can I do  
> something  
> like this? (doesn’t work)  
> find :all, :conditions =\> [‘rating \>= ?’, user.threshold] #  
> NoMethodError: undefined method user for Article:class  
> end  
> end  
> end
> 
> How would I implement this? As the error suggests, the problem is that  
> the  
> article doesn’t know who its user is. So how can I pass that information  
> to  
> this method, given that it’s nested inside of this association?

The code in an association extension is executed in the context of the  
AssociationProxy object, not the ActiveRecord model. Simply put, that  
means that “self” in the extension isn’t the user, it’s the has\_many  
proxy. But proxy\_owner points to the user, so you can do  
`proxy_owner.threshold` to get the user’s threshold.

–  
Josh S.  
[http://blog.hasmanythrough.com/](http://blog.hasmanythrough.com/)

---

<div class="post-metadata">

**Author:** ![latte](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/5e9695/32.png) [@latte](https://www.ruby-forum.com/u/latte)\
**Post date:** [January 31, 2007, 8:23pm UTC](https://www.ruby-forum.com/t/help-with-extensions/84026/3 "2007-01-31T20:23:58Z")

</div>

Thanks for this – exactly what I was looking for. .

Andy

On Jan 31, 11:47 am, Josh S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
