Forum: Ruby on Rails help with extensions

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.
Cd9b5c14565e85b8877b9dabbbf78368?d=identicon&s=25 Andy Eggers (Guest)
on 2007-01-31 16:51
(Received via mailing list)
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 Development: 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 Groups page get lost
sometimes
-- sorry if this is double-posted.)
9f0f89bbd9e1ecfbaab6584e429b7a2f?d=identicon&s=25 Josh Susser (jsusser)
on 2007-01-31 17:47
Andy Eggers 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 Susser
http://blog.hasmanythrough.com/
Cd9b5c14565e85b8877b9dabbbf78368?d=identicon&s=25 andy (Guest)
on 2007-01-31 20:23
(Received via mailing list)
Thanks for this -- exactly what I was looking for. .

Andy

On Jan 31, 11:47 am, Josh Susser <rails-mailing-l...@andreas-s.net>
This topic is locked and can not be replied to.