mirror of https://github.com/rails/rails
Clarify possible validations you can use to avoid a NULL boolean value
in the database. Closes #16304 [ci skip]
This commit is contained in:
parent
44e6d91a07
commit
a4d8b62ed1
|
@ -524,9 +524,15 @@ If you validate the presence of an object associated via a `has_one` or
|
||||||
`marked_for_destruction?`.
|
`marked_for_destruction?`.
|
||||||
|
|
||||||
Since `false.blank?` is true, if you want to validate the presence of a boolean
|
Since `false.blank?` is true, if you want to validate the presence of a boolean
|
||||||
field you should use `validates :field_name, inclusion: { in: [true, false] }`.
|
field you should use one of the following validations:
|
||||||
|
|
||||||
The default error message is _"can't be blank"_.
|
```ruby
|
||||||
|
validates :boolean_field_name, presence: true
|
||||||
|
validates :boolean_field_name, inclusion: { in: [true, false] }
|
||||||
|
validates :boolean_field_name, exclusion: { in: [nil] }
|
||||||
|
```
|
||||||
|
By using one of these validations, you will ensure the value will NOT be `nil`
|
||||||
|
which would result in a `NULL` value in most cases.
|
||||||
|
|
||||||
### `absence`
|
### `absence`
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue