On Nov 19, 6:08 pm, Marcel Hild <[EMAIL PROTECTED]> wrote:
> Hi,
> I am trying to overwrite the accessor of a has_many association.
> Basically my class looks like this:
>
> class Person
> has_many :emails
> has_one :link, :class_name => 'PersonProxy'
>
> def emails_with_link
> link.emails + emails_without_link
> end
> alias_method_chain :emails, :link
> end
>
> Now that works fine if I only access the collection like>>
> Person.find(1).emails
>
> => [#<Email...]
>
> But using any collection specific methods throw a NoMethodError>>
> Person.find(1).emails.build
>
> => NoMethodError: undefined method `build' for #<Array:0x226c634>
>
> which makes sense, as I return an array...
>
The trick is that link.emails isn't an array - it's an association
proxy.
I'm not sure what the write thing to do here is. Maybe something like
has_many :emails do
def load_target
super
proxy_target += proxy_owner.link.emails
end
end
might do the trick ?
Fred
> But how do I return the Association? Or are there any other tricks to
> implement this decorator for associations?
>
> Cheers
> Marcel
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---