mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
create
return an active record object with erros instead of false when validation fails. [skip ci]
This commit is contained in:
parent
08754f12e6
commit
d0af905925
1 changed files with 5 additions and 4 deletions
|
@ -310,10 +310,10 @@ models and validate that an attribute value is not empty, is unique and not
|
|||
already in the database, follows a specific format and many more.
|
||||
|
||||
Validation is a very important issue to consider when persisting to the database, so
|
||||
the methods `create`, `save` and `update` take it into account when
|
||||
the methods `save` and `update` take it into account when
|
||||
running: they return `false` when validation fails and they didn't actually
|
||||
perform any operation on the database. All of these have a bang counterpart (that
|
||||
is, `create!`, `save!` and `update!`), which are stricter in that
|
||||
is, `save!` and `update!`), which are stricter in that
|
||||
they raise the exception `ActiveRecord::RecordInvalid` if validation fails.
|
||||
A quick example to illustrate:
|
||||
|
||||
|
@ -322,8 +322,9 @@ class User < ActiveRecord::Base
|
|||
validates :name, presence: true
|
||||
end
|
||||
|
||||
User.create # => false
|
||||
User.create! # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
|
||||
user = User.new
|
||||
user.save # => false
|
||||
user.save! # => ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
|
||||
```
|
||||
|
||||
You can learn more about validations in the [Active Record Validations
|
||||
|
|
Loading…
Reference in a new issue