# Rails and MySQL boolean types

**URL:** <https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325>\
**Category:** Rails\
**Created:** [December 24, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325 "2008-12-24T15:37:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 24, 2008, 3:37pm UTC](https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325/1 "2008-12-24T15:37:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 24, 2008, 4:06pm UTC](https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325/2 "2008-12-24T16:06:19Z")

</div>

On 24 Dec 2008, at 14:37, sa 125 wrote:

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

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 24, 2008, 4:22pm UTC](https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325/3 "2008-12-24T16:22:58Z")

</div>

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)?

---

<div class="post-metadata">

**Author:** ![sa125](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/b4bc9f/32.png) [@sa125](https://www.ruby-forum.com/u/sa125)\
**Post date:** [December 25, 2008, 1:34am UTC](https://www.ruby-forum.com/t/rails-and-mysql-boolean-types/156325/4 "2008-12-25T01:34:58Z")

</div>

2008/12/24 sa 125 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> 

Probably you can have something like that:

validates\_inclusion\_of :in\_stock =\> [true, false]
