Skip to content

Commit

Permalink
[DOC] Update Active Record basic guide for create method in case of v…
Browse files Browse the repository at this point in the history
…alidation fails
  • Loading branch information
maniSHarma7575 committed Jul 15, 2024
1 parent 4867559 commit aadb414
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions guides/source/active_record_basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -586,11 +586,12 @@ unique, is not already in the database, follows a specific format, and many
more.

Methods like `save`, `create` and `update` validate a model before persisting it
to the database. When a model is invalid these methods return `false` and no
database operations are performed. All of these methods have a bang counterpart
(that is, `save!`, `create!` and `update!`), which are stricter in that they
raise an `ActiveRecord::RecordInvalid` exception when validation fails. A quick
example to illustrate:
to the database. If the model is invalid, no database operations are performed. In
this case the `save` and `update` methods return `false`. The `create` method still
returns the object, which can be checked for errors. All of these
methods have a bang counterpart (that is, `save!`, `create!` and `update!`),
which are stricter in that they raise an `ActiveRecord::RecordInvalid` exception
when validation fails. A quick example to illustrate:

```ruby
class User < ApplicationRecord
Expand All @@ -606,6 +607,16 @@ irb> user.save!
ActiveRecord::RecordInvalid: Validation failed: Name can't be blank
```

The `create` method always returns an object, regardless of
its validity. You can then inspect this object for any errors.

```irb
irb> user = User.create
=> #<User:0x000000013e8b5008 id: nil, name: nil>
irb> user.errors.full_messages
=> ["Name can't be blank"]
```

You can learn more about validations in the [Active Record Validations
guide](active_record_validations.html).

Expand Down

0 comments on commit aadb414

Please sign in to comment.