diff options
author | Arthur Nogueira Neves <arthurnn@gmail.com> | 2014-07-23 13:02:12 -0400 |
---|---|---|
committer | Arthur Nogueira Neves <arthurnn@gmail.com> | 2014-07-23 13:02:12 -0400 |
commit | 8c48a708243236491e33290e3b5676dc27a64c05 (patch) | |
tree | 6978394c9a0a75b74685e980cb404bee0555ee4f /guides | |
parent | 74a157ac5f0955ce3472ddeca578a9f2d69b0a98 (diff) | |
parent | d0af90592540225080d03ea2384da759f18620ff (diff) | |
download | rails-8c48a708243236491e33290e3b5676dc27a64c05.tar.gz rails-8c48a708243236491e33290e3b5676dc27a64c05.tar.bz2 rails-8c48a708243236491e33290e3b5676dc27a64c05.zip |
Merge pull request #16237 from tonytonyjan/patch
`create` method returns an active record object with errors instead of false when validation fails. [skip ci]
Diffstat (limited to 'guides')
-rw-r--r-- | guides/source/active_record_basics.md | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md index 21022f1abb..eff93ce41d 100644 --- a/guides/source/active_record_basics.md +++ b/guides/source/active_record_basics.md @@ -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 |