As per http://rails.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html Possible callbacks are: before_add, after_add, before_remove and after_remove. Should any of the before_add callbacks throw an exception, the object does not get added to the collection. Same with the before_remove callbacks; if an exception is thrown the object doesnât get removed. However, when the add fails due to a before_add violation, the exception thrown just blows up the whole stack. It doesn''t get caught and turned into a Validation error on the object. Is there a standard pattern for handling this? Thanks, -cpr -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Lee Smith
2011-Feb-28 23:28 UTC
Re: How to turn :before_add exceptions into validation errors
I''m having the same problem with a :before_add association callback. However, I''m actually adding the error directly to my model THEN raising the exception. But like you found, somewhere in the stack the model''s errors get cleared or something and I don''t see the error that I manually added to my model. https://gist.github.com/847748 Could really use some help with this...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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.