Class: ActiveModel::Validations::BooleanPresenceValidator
- Inherits:
-
EachValidator
- Object
- EachValidator
- ActiveModel::Validations::BooleanPresenceValidator
- Defined in:
- lib/activemodel-validators/boolean_presence_validator.rb
Instance Method Summary collapse
-
#validate_each(record, attribute, value) ⇒ Object
You can’t use validates :presence for boolean attributes! You could do this instead: validates_inclusion_of :attr, :in => [true, false] but the main reason to use this validator instead of is that the error message is better (“not in list? what list?”) and the meaning is clearer when reading the code (we’re validating the presence of a boolean attribute – true or false).
Instance Method Details
#validate_each(record, attribute, value) ⇒ Object
You can’t use validates :presence for boolean attributes! You could do this instead:
validates_inclusion_of :attr, :in => [true, false]
but the main reason to use this validator instead of is that the error message is better (“not in list? what list?”) and the meaning is clearer when reading the code (we’re validating the presence of a boolean attribute – true or false).
10 11 12 13 14 |
# File 'lib/activemodel-validators/boolean_presence_validator.rb', line 10 def validate_each(record, attribute, value) unless [true, false].include? value record.errors.add attribute, :blank end end |