# How do booleans work?

**URL:** <https://www.ruby-forum.com/t/how-do-booleans-work/156893>\
**Category:** Rails\
**Created:** [January 5, 2009, 1:26am UTC](https://www.ruby-forum.com/t/how-do-booleans-work/156893 "2009-01-05T01:26:01Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [January 5, 2009, 1:26am UTC](https://www.ruby-forum.com/t/how-do-booleans-work/156893/1 "2009-01-05T01:26:01Z")

</div>

I’m a little confused as to how booleans work in Rails. Which is  
better in the database? boolean or int(1)? True == 1 and False == 0,  
right? Using SQLite, I tried to set a value to true inside the  
database but it gave me an error. MySql didn’t, so I had to switch to  
int(1) for it to work. Is this just how it is?

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [January 5, 2009, 2:01am UTC](https://www.ruby-forum.com/t/how-do-booleans-work/156893/2 "2009-01-05T02:01:44Z")

</div>

Mike Chai wrote:

> I’m a little confused as to how booleans work in Rails. Which is  
> better in the database? boolean or int(1)? True == 1 and False == 0,  
> right? Using SQLite, I tried to set a value to true inside the  
> database but it gave me an error. MySql didn’t, so I had to switch to  
> int(1) for it to work. Is this just how it is?

No. True == anything other than nil or false  
False == False

DBMS have their own specific ways of handling booleans. The DBMS  
adapter handles converting that to a Ruby form.

---

<div class="post-metadata">

**Author:** ![mikec](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/977dab/32.png) [@mikec](https://www.ruby-forum.com/u/mikec)\
**Post date:** [January 5, 2009, 4:15am UTC](https://www.ruby-forum.com/t/how-do-booleans-work/156893/3 "2009-01-05T04:15:09Z")

</div>

What Mike is hinting at is the way ActiveRecord abstracts database  
differences. MySQL and SQLite3 probably use different column types but  
this will appear transparent to the developer as ActiveRecord handles  
serializing and de-serializing the column data into Ruby objects.

I am not a DBA, and I’m sure it varies from DB vendor to vendor, but I  
would have to assume that the ActiveRecord Developers chose the most  
appropriate column format (int(1) vs boolean) for each supported  
database.

On Jan 4, 7:01 pm, James B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
