Is there anyway I can prevent the collection<<(object, ...) and similar methods from adding objects that do not satisfy a specific condition. This would be similar to the :condition option on the has_and_belongs_to_many association. I want to somehow place a condition or rewrite collection<<(object, ...) and similar methods so that when an "incompatible" object is attempted to be added to the collection, it returns false or throws an exception. Is this possible? Thanks! --~--~---------~--~----~------------~-------~--~----~ 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 Jan 30, 2008 11:37 PM, apramanik <abhikp-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I want to somehow place a condition or rewrite > collection<<(object, ...) and similar methods so that when an > "incompatible" object is attempted to be added to the collection, it > returns false or throws an exception. Is this possible?Sounds like you want validates_associated. http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMethods.html#M001334 -- Greg Donald http://destiney.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 -~----------~----~----~----~------~----~------~--~---
It was a condition relating the record with the association and the associated record. I ended up using the :before_add option and raising an exception (which rolls back the transaction). Thanks! On Feb 1, 12:09 pm, "Greg Donald" <gdon...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 30, 2008 11:37 PM, apramanik <abh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I want to somehow place a condition or rewrite > > collection<<(object, ...) and similar methods so that when an > > "incompatible" object is attempted to be added to the collection, it > > returns false or throws an exception. Is this possible? > > Sounds like you want validates_associated. > > http://api.rubyonrails.com/classes/ActiveRecord/Validations/ClassMeth... > > -- > Greg Donaldhttp://destiney.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 -~----------~----~----~----~------~----~------~--~---