How to turn :before_add exceptions into validation errors

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

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!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs