Should ActiveRecord::RecordNotSaved attach the record in question?

I'm doing some work where I want to rescue ActiveRecord::RecordNotSaved from a save! call nested deep in the code. The thing is there could theoretically be failures here I want to pass through, and I want to inspect the record in order to decide.

I would usually not recommend using exceptions for program flow [1], especially for things like validation errors.

Keeping this in mind, here is what I could think of: 1) save using the regular (non-!) method 2) inspect the errors on the object 3) throw an exception only if you are sure you want to abort your current action.

I was thinking of whipping up a patch, but I wanted to ask here if there is some reason we definitely don't want AR records attached to exceptions (memory leak risk or some other consider I'm not aware of).

The ActiveRecord::RecordInvalid exception already has a link to the model on which save was attempted. The object can be fetched via the .record method on the exception.

If you are really keen on using exceptions, you could potentially catch the exception, inspect the errors as I described above, and then re-raise the exception if you want to abort.

[1] http://c2.com/cgi/wiki?DontUseExceptionsForFlowControl