Forum: Ruby on Rails boolean db fields set to false fail validation with validate

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
125639ddcc0b9119bb84e29416ace63a?d=identicon&s=25 Scott (Guest)
on 2007-02-27 00:44
(Received via mailing list)
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
125639ddcc0b9119bb84e29416ace63a?d=identicon&s=25 Scott (Guest)
on 2007-02-27 15:10
(Received via mailing list)
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
2017657725dd1bce83dc8a1e2e991d04?d=identicon&s=25 Luke Ivers (Guest)
on 2007-02-27 16:24
(Received via mailing list)
See
http://api.rubyonrails.org/classes/ActiveRecord/Va...

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]
This topic is locked and can not be replied to.