Or little computing tricks and hacks
Data Validation of boolean values in Rails
April 24, 2015Posted by on
To validating the presence of a boolean value, the short answer is you don’t need to. Rails does an automatic check on boolean data types.
In theory validating the presence would be just a waste of time, but in practice it creates a bug. If you have in your
model.rb a line similar to
validates :is_active, presence: true
is_active is a boolean, when entering
false as a value, Rails interprets it as empty and the action of creating or updating the record is refused.
Leaving the line out will allow the creation or updating of the record and if the value is not either
false, the stored value will be
You may want to ensure that the value is either
false. In that case, the following line should be added:
validates_inclusion_of :active, :in => [true, false]