mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Add lambdas in conditional validations
As `Lambdas` are a type of `Proc`, they can also be used in the `if`/`unless` option of a validation to decide when the validation is executed. Add this case to the guide for clarification. Closes https://github.com/rails/rails/issues/33212
This commit is contained in:
parent
b9807eb538
commit
3cb491f327
1 changed files with 7 additions and 0 deletions
|
@ -927,6 +927,13 @@ class Account < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
|
As `Lambdas` are a type of `Proc`, they can also be used to write inline
|
||||||
|
conditions in a shorter way.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
validates :password, confirmation: true, unless: -> { password.blank? }
|
||||||
|
```
|
||||||
|
|
||||||
### Grouping Conditional validations
|
### Grouping Conditional validations
|
||||||
|
|
||||||
Sometimes it is useful to have multiple validations use one condition. It can
|
Sometimes it is useful to have multiple validations use one condition. It can
|
||||||
|
|
Loading…
Reference in a new issue