ActiveRecord validates_presence_of with false

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 :slight_smile:

Thank you


Hendy I.

Hendy I. wrote:

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

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_presence_of-with-false-tf4161367.html#a11843712
Sent from the RubyOnRails Users mailing list archive at Nabble.com.