How do booleans work?

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?

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.

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. [email protected]