Fix active_record_validations.md document, `:save` for `on:` validation helper was never available
According to the guide, ":save" value for the "on:" validation helper was available like below validates :name, presence: true, on: :save but this was never available according to the implementation of the valid? method, which is below # Runs all the validations within the specified context. Returns +true+ if # no errors are found, +false+ otherwise. # # If the argument is +false+ (default is +nil+), the context is set to <tt>:create</tt> if # <tt>new_record?</tt> is +true+, and to <tt>:update</tt> if it is not. # # Validations with no <tt>:on</tt> option will run no matter the context. Validations with # some <tt>:on</tt> option will only run in the specified context. def valid?(context = nil) context ||= (new_record? ? :create : :update) output = super(context) errors.empty? && output end So the documentation was always wrong since the PR proposed by @neerajdotname ( #10287 ) was rejected.
This commit is contained in:
parent
b67a80de9b
commit
740f7787e0
|
@ -243,7 +243,7 @@ line of code you can add the same kind of validation to several attributes.
|
||||||
All of them accept the `:on` and `:message` options, which define when the
|
All of them accept the `:on` and `:message` options, which define when the
|
||||||
validation should be run and what message should be added to the `errors`
|
validation should be run and what message should be added to the `errors`
|
||||||
collection if it fails, respectively. The `:on` option takes one of the values
|
collection if it fails, respectively. The `:on` option takes one of the values
|
||||||
`:save` (the default), `:create` or `:update`. There is a default error
|
`:create` or `:update`. There is a default error
|
||||||
message for each one of the validation helpers. These messages are used when
|
message for each one of the validation helpers. These messages are used when
|
||||||
the `:message` option isn't specified. Let's take a look at each one of the
|
the `:message` option isn't specified. Let's take a look at each one of the
|
||||||
available helpers.
|
available helpers.
|
||||||
|
@ -765,10 +765,9 @@ class Person < ActiveRecord::Base
|
||||||
validates :age, numericality: true, on: :update
|
validates :age, numericality: true, on: :update
|
||||||
|
|
||||||
# the default (validates on both create and update)
|
# the default (validates on both create and update)
|
||||||
validates :name, presence: true, on: :save
|
validates :name, presence: true
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
The last line is in review state and as of now, it is not running in any version of Rails 3.2.x as discussed in this [issue](https://github.com/rails/rails/issues/10248)
|
|
||||||
|
|
||||||
Strict Validations
|
Strict Validations
|
||||||
------------------
|
------------------
|
||||||
|
|
Loading…
Reference in New Issue