Intriguing Behavior: Validating Presence of a Boolean Field

So, for those who don’t know it (I just discovered about it), if you want to validate the presence of a boolean field, you can’t do it with validates_presence_of.

So, suppose that we have a model like


# == Schema Information
# Table name: Bla
#  test       :boolean
class Bla < ActiveRecord::Base
  attr_accessible :test

For example, if I had used validates_presence_of :test in the example above, it would have failed miserably because false.blank? is true – so in practice, we could have never set bla.test to false, since it would make the validation fail (more details here).

So I set out to try this out:


validates_inclusion_of :test, :in => [true, false]

and test it like this:


it {should_not allow_value("bla").for(:completed)}

and it still failed miserably.

Now I was puzzled… So I tried this one out:

I was thinking that at some point the test attribute would be set to “bla”, but since the field is a boolean, inserting other values will always evaluate to false. So we don’t have to go to all this trouble: this way we can validate the presence, but we never need to validate whether the value itself is valid. More about this here. Living and learning!

Comments