Skip to content

Commit 6ccc4d1

Browse files
committed
add validate section
1 parent 732602d commit 6ccc4d1

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

guides/source/active_record_validations.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -932,6 +932,27 @@ See [`validates_uniqueness_of`][] for more information.
932932
[the PostgreSQL manual]:
933933
https://www.postgresql.org/docs/current/static/ddl-constraints.html
934934

935+
### `validate`
936+
937+
The `validate` validator is used with `belongs_to` and `has_one` associations to
938+
automatically validate the associated object whenever the parent object is
939+
saved. This option ensures that any new or updated associated objects are
940+
validated before the parent object can be successfully saved. By default, this
941+
option is set to `false`, meaning associated objects will not be automatically
942+
validated.
943+
944+
```ruby
945+
class Post < ApplicationRecord
946+
belongs_to :author, validate: true
947+
end
948+
```
949+
950+
In this example, every time a `Post` is saved, the associated `Author` will also
951+
be validated. If the `Author` is invalid, the `Post` will not be saved.
952+
953+
If you need to ensure that associated objects are validated every time the
954+
parent object is updated, consider using `validates_associated`.
955+
935956
### `validates_associated`
936957

937958
You should use this validator when your model has associations that always need to

0 commit comments

Comments
 (0)