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


http://api.rubyonrails.org/classes/ActiveRecord/Persistence/ClassMethods.html#method-i-create-21
Allen Madsen
http://www.allenmadsen.com


On Wed, Feb 17, 2016 at 2:40 PM, Daniel Finlay <[email protected]> wrote:
> 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.
>
> Thoughts?
>
>
> 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 received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at https://groups.google.com/group/rubyonrails-core.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Core" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/rubyonrails-core.
For more options, visit https://groups.google.com/d/optout.

Reply via email to