Rails and MySQL boolean types

I'm trying to create a model that uses a boolean type, supported by mysql engine. My generate is like so:

./script/generate model Product name:string vendor_id:integer in_stock:boolean

However, when I try to play with the console and save products to the database, only products defined with in_stock = true or in_stock = 1 are successfully saved. I can't seem to save items with a false value. Here's what I mean:

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => true)

=> #<Product id:nil, name: "VCR", vendor_id: 1, in_stock: true, created_at....>

p.save

=> true

This also works with :in_use => 1

vHowever, all these fail:

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => false) p.save

=> false

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => 0) p.save

=> false

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => nil)>> p.save

=> false

The migration lists the type as t.boolean :in_use, and the database shows the column type as tinyint(1).

What am I doing wrong?

Thanks!

vHowever, all these fail:

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => false) p.save

=> false

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock => 0) p.save

=> false

p = Product.new(:name => "VCR", :vendor_id => 1, :in_stock =>
nil)>> p.save

=> false

The migration lists the type as t.boolean :in_use, and the database shows the column type as tinyint(1).

That's normal. Dou have a validation that this is causing the save to
fail ?

Fred

You were right, I had validates_presence_of :name, :vendor_id, :in_stock inside my model. Once I removed it I was able to save false values. How can I still validate a boolean once I allow a user to enter values (even though it'll be either one or the other, using a checkbox or something)?

Probably you can have something like that:

validates_inclusion_of :in_stock => [true, false]