aboutsummaryrefslogtreecommitdiffstats
path: root/guides
diff options
context:
space:
mode:
authorAaron Sumner <asumner@oreilly.com>2018-07-11 16:21:08 -0700
committerAaron Sumner <asumner@oreilly.com>2018-07-11 16:22:49 -0700
commit4216e93e1a4ba7e1d5ace8de628e8bbdf4a661e6 (patch)
tree301e57874b979ce9ebbcc4c51596935da131f10f /guides
parentf16fdefe3243978aef11204b12c25b86c022ca7a (diff)
downloadrails-4216e93e1a4ba7e1d5ace8de628e8bbdf4a661e6.tar.gz
rails-4216e93e1a4ba7e1d5ace8de628e8bbdf4a661e6.tar.bz2
rails-4216e93e1a4ba7e1d5ace8de628e8bbdf4a661e6.zip
Update guide for validation custom contexts [ci skip]
The Active Record validations guide's section on custom contexts appears to be incomplete. the code sample shows a context being added to validations, but not being used. Add to the sample code for this section by showing validations being run with and without the custom context. Add a second sample code block showing that validations with no context are also run, when a context is used.
Diffstat (limited to 'guides')
-rw-r--r--guides/source/active_record_validations.md36
1 files changed, 27 insertions, 9 deletions
diff --git a/guides/source/active_record_validations.md b/guides/source/active_record_validations.md
index c7846a0283..9a652f289c 100644
--- a/guides/source/active_record_validations.md
+++ b/guides/source/active_record_validations.md
@@ -844,9 +844,9 @@ class Person < ApplicationRecord
end
```
-You can also use `on:` to define custom context.
-Custom contexts need to be triggered explicitly
-by passing name of the context to `valid?`, `invalid?` or `save`.
+You can also use `on:` to define custom contexts. Custom contexts need to be
+triggered explicitly by passing the name of the context to `valid?`,
+`invalid?`, or `save`.
```ruby
class Person < ApplicationRecord
@@ -854,14 +854,32 @@ class Person < ApplicationRecord
validates :age, numericality: true, on: :account_setup
end
-person = Person.new
+person = Person.new(age: 'thirty-three')
+person.valid? # => true
+person.valid?(:account_setup) # => false
+person.errors.messages
+ # => {:email=>["has already been taken"], :age=>["is not a number"]}
```
-`person.valid?(:account_setup)` executes both the validations
-without saving the model. And `person.save(context: :account_setup)`
-validates `person` in `account_setup` context before saving.
-On explicit triggers, model is validated by
-validations of only that context and validations without context.
+`person.valid?(:account_setup)` executes both the validations without saving
+the model. `person.save(context: :account_setup)` validates `person` in the
+`account_setup` context before saving.
+
+When triggered by an explicit context, validations are run for that context,
+as well as any validations _without_ a context.
+
+```ruby
+class Person < ApplicationRecord
+ validates :email, uniqueness: true, on: :account_setup
+ validates :age, numericality: true, on: :account_setup
+ validates :name, presence: true
+end
+
+person = Person.new
+person.valid?(:account_setup) # => false
+person.errors.messages
+ # => {:email=>["has already been taken"], :age=>["is not a number"], :name=>["can't be blank"]}
+```
Strict Validations
------------------