Boolean db fields set to false fail validation with validate


#1

I have a model that contains a :boolean field. In my view, I have a
simple select of Yes or No, which equates to true or false,
respectively. If I choose yes and save, everything works great. If I
choose no, my model fails validation as I have this set as a required
field using validates_presence_of. I have checked the log and it’s
definitely set to false, why would this fail validation?

Thanks,

Scott


#2

Anyone else seeing this? If not, would someone mind sending me a
snippet of a view/model that used a boolean that doesn’t suffer from
this?

Thanks,

Scott


#3

See
http://api.rubyonrails.org/classes/ActiveRecord/Validations/ClassMethods.html#M000941

It basically says that due to some other bits of AR magic, you have to
validate_presence_of kind of funky on boolean variables:
validates_inclusion_of :field_name, :in => [true, false]