A little gotcha about validating presence of boolean fields from the doc:

If you want to validate the presence of a boolean field (where the real values are true and false), you will want to use validates_inclusion_of :field_name, :in => [true, false] This is due to the way Object#blank? handles boolean values. false.blank? # => true.

I personally find this a bit misleading. To me validate_presence_of implies that a check to see if there’s a value assigned. As far as I know nil is used to indicate the lack of such value. Furthermore, false.blank? is true which I think is wrong because false is a value.

The whole meaning of blank? seem to start loosing its value when you consider that nil.blank? == [].blank? == {}.blank? == "".blank? == false.blank? == true. I think there’s a much better way to handle a check for emptiness, or rather a default value of something. I’m also a strong believer that nil.anything should raise an exception… but that’s just my C roots talking.

One Response to “Validating presence of a boolean field”
  1. David Says:

    Thanks for the entry. I just hit my head against a wall for half an hour trying to understand why the hell this was happening

Leave a Reply