I need to validate boolean fields for a Rails ActiveRecord object. I used the common solution to this problem:
validates :boolean_field, :inclusion => { :in => [true, false] }
But although this solutions solves the problem with validating false
values, it still allows to save strings to the boolean_field
:
my_object.boolean_field = "string"
my_object.save # => true
my_object.boolean_field # => false
Why does this happen if I specified the inclusion to [true, false]
? And more importantly, how do I validate only true and false values?
P.S. If it is important to the question, I'm using PostgreSQL.