Why does false invalidate validates_presence_of? Why does false invalidate validates_presence_of? ruby-on-rails ruby-on-rails

Why does false invalidate validates_presence_of?


See the API docs...

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].


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

no longer works for some versions after 1.3.0 of the shoulda matchers when you are testing that only Boolean values are accepted by your model.

Instead, you should do something like this:

it { should allow_value(true).for(:your_field) }  it { should allow_value(false).for(:your_field) }it { should_not allow_value(nil).for(:your_field) }

You can see the discussion here.

There was a partial fix for this that now warns if you are trying to do this here