ActiveRecord should have a create-like method that throws 400 on invalid record

I’d like to propose and maybe start on a patch that adds a new ActiveRecord method, maybe named make, that behaves exactly like create, except in the case of failed validations, it throws an ActiveRecord::BadRequest error, that the controller will automatically convert into a 400 response along with the record’s errors array.

This could potentially save 5 lines of code in every create route of every Rails application, where the record is checked for validity and conditionally a 400 error is thrown.

This would be similar to the find method, in that it allows the controller implementor to trust the models to throw errors that are converted into the appropriate HTTP StatusCodes.


PS. I think I may have submitted this already, but I don’t see it in the main list, so I’m re-submitting as a way of checking if I did something wrong.

You can already do this. Call create! and rescue ActiveRecord::RecordInvalid. Allen Madsen