File tree Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Expand file tree Collapse file tree 1 file changed +21
-0
lines changed Original file line number Diff line number Diff line change @@ -932,6 +932,27 @@ See [`validates_uniqueness_of`][] for more information.
932
932
[ the PostgreSQL manual] :
933
933
https://www.postgresql.org/docs/current/static/ddl-constraints.html
934
934
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
+
935
956
### ` validates_associated `
936
957
937
958
You should use this validator when your model has associations that always need to
You can’t perform that action at this time.
0 commit comments