problem with passing booleans to update_attributes problem with passing booleans to update_attributes ruby ruby

problem with passing booleans to update_attributes


Your model is actually behaving exactly as you told it to, through your use of validates :orderable, :presence => true

There's little point validating the presence of a boolean flag - it's going to be true, nil or false - and in Ruby world, nil and false have the same semantic value when it comes to boolean logic.

Internally, validates :presence relies on the value of the attribute being checked to return false when blank? is called. And, in Rails (with ActiveSupport), false.blank? evaluates as true - which means that your field is failing the validation.

Simply remove that validation and everything will work as expected.


Like Dan Cheail already said in his answer, a nil and false boolean is semantically the same thing.

But, if you really need to validate it (not allowing nil), you can always do :

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


Instead of validates :presence => :true, you should write your migrations with the default value like this:

t.boolean :orderable, :default => 0

I assume your default value should be false. If true, use 1 as default. Then it will set the default value in database. So, you can omit the validation check.

The reason you cannot use validates :presence is answered by @dan. Presence means not blank and Rails use .blank? function for this and false.blank? is true