Hi All,
I''ve got a situation where I want a has_many :through association to
behave
somewhat like a normal has_many when << is called on the association. ie
I
would like it to return false instead of throwing an exception.
With some inspiration from http://blog.hasmanythrough.com I''ve come up
with,
class Container < AR:B
has_many :container_items
has_many :items, :through => :container_items do
def <<( item )
begin
self.concat item
rescue ActiveRecord::RecordInvalid => e
@owner.errors.add( :item, e)
false
end
end
end
Is there anything that stands out as a stark issue with doing this? I would
like to call owner in some other way but I have not found how to get at
@owner with a method.
The join model has only timestamps in addition to the two f''keys
Any comments, insights, crits etc are welcome.
I''ve extracted this into a plugin if anyone would like to try it. I
can''t
make any guarantees though.
http://svn.devjavu.com/liquid/has_many_through_append_fix
Cheers
Daniel
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Daniel ----- wrote:> I''ve got a situation where I want a has_many :through association to > behave > somewhat like a normal has_many when << is called on the association. > ie I > would like it to return false instead of throwing an exception. > > With some inspiration from http://blog.hasmanythrough.com I''ve come up > with, > > class Container < AR:B > > has_many :container_items > has_many :items, :through => :container_items do > def <<( item ) > begin > self.concat item > rescue ActiveRecord::RecordInvalid => e > @owner.errors.add( :item, e) > false > end > end > end > > > Is there anything that stands out as a stark issue with doing this? I > would > like to call owner in some other way but I have not found how to get at > @owner with a method.Looks like it might work to me. You can get the association owner in an extension with the proxy_owner method. -- Josh Susser http://blog.hasmanythrough.com -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 4/14/07, Josh Susser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > > Looks like it might work to me. You can get the association owner in an > extension with the proxy_owner method. > > -- > Josh Susser > http://blog.hasmanythrough.comThanx Josh, I was hopeing you might take a look. I''ll have a go at using an extension for this when I get home. Cheers --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---