Forum: Ruby on Rails ActiveRecord validates_presence_of with false

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.
Hendy I. (Guest)
on 2007-07-28 10:42
(Received via mailing list)
Hi Rails

I can't seem to use a boolean value 'false' with validates_presence_of.
It always fails.

I even tried using 'f'.

What's the proper way to set a false value?

Isn't false a "present" value in a boolean column?

What I want is to require either true or false, but prevent nil :)

Thank you

--
Hendy I.
www.hendyirawan.com
Ryan Carmelo Briones (Guest)
on 2007-07-28 19:20
(Received via mailing list)
Hendy I. wrote:
>
>
> What I want is to require either true or false, but prevent nil :)
>
>

From the API docs for validates_presence_of:

"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"

This should do the trick!

---
Ryan Carmelo Briones

--
View this message in context:
http://www.nabble.com/ActiveRecord-validates_prese...
Sent from the RubyOnRails Users mailing list archive at Nabble.com.
This topic is locked and can not be replied to.