Wednesday, September 30, 2009

ActiveRecord::RecordInvalid vs ActiveRecord::RecordNotSaved

Ever wonder why calling save! sometimes raises ActiveRecord::RecordInvalid and sometimes raises ActiveRecord::RecordNotSaved?

From rails documentation:

If any of validations fail ActiveRecord::RecordInvalid gets raised.
If any of the before_* callbacks return false the action is cancelled and save! raises ActiveRecord::RecordNotSaved.

So if you have before_* filters on your model, remember to catch ActiveRecord::RecordNotSaved in addition to ActiveRecord::RecordInvalid

No comments: