# Does ActiveRecord have support for "Boolean" columns?

**URL:** <https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849>\
**Category:** Rails\
**Created:** [April 4, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849 "2006-04-04T22:31:18Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 4, 2006, 10:31pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/1 "2006-04-04T22:31:18Z")

</div>

Hey all –

I’ve been using enumerable char(1)'s with ‘y’ and ‘n’ values for my  
Boolean columns in Rails, but it strikes me there should be a better  
way.

How do you setup columns that represent true/false-ness in your  
schemas on Rails?

Wondering what kind of standard approaches there are other than me  
writing something like

class MyEntity \< ActiveRecord::Base  
def active  
return false unless active==‘y’  
true  
end  
end

– Brendan Baldwin (dot com)

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 4, 2006, 11:08pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/2 "2006-04-04T23:08:25Z")

</div>

> I’ve been using enumerable char(1)'s with ‘y’ and ‘n’ values for my  
> Boolean columns in Rails, but it strikes me there should be a better  
> way.

Just sharing, cuz I haven’t found a better way than yours – I use  
tinyints, set after my migration has run, of course…

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 4, 2006, 11:11pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/3 "2006-04-04T23:11:06Z")

</div>

If you use migrations to create your database, you can specify boolean  
columns:

class CreateUsers \< ActiveRecord::Migration  
def self.up  
create\_table :users do |table|  
table.column :username, :string, :limit =\> 32, :null =\> false  
table.column :active, :boolean  
end  
end

def self.down  
drop\_table :users  
end  
end

using MySQL for the DB, it creates a TINYINT(1) column, in which is  
stores 0  
or 1. ActiveRecord then treats it as a boolean.

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 4, 2006, 11:15pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/4 "2006-04-04T23:15:02Z")

</div>

> using MySQL for the DB, it creates a TINYINT(1) column, in which is  
> stores 0  
> or 1. ActiveRecord then treats it as a boolean.

Well, how ace is that?  
Excuse me – I have some migrations editing to do…  
Cheers, man!

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 4, 2006, 11:16pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/5 "2006-04-04T23:16:13Z")

</div>

Rails treats an integer field with 0 and 1 as a boolean. The  
checkbox\_field helpers know what to do with 0 and 1 values, and scaffold  
generators create pulldowns for those fields with “True” and “False” on  
the labels and 0 and 1 as the return values.

If you use migrations to maintain your database schema, a command like

```
add_column :salespeople, :has_been_trained, :boolean, :default => 

```

false

will create the appropriate field type for you. On MySQL this generates  
a tinyint(1) in the salespeople table, with a default of 0.

Brendan Baldwin wrote:

> Hey all –
> 
> I’ve been using enumerable char(1)'s with ‘y’ and ‘n’ values for my  
> Boolean columns in Rails, but it strikes me there should be a better  
> way.
> 
> How do you setup columns that represent true/false-ness in your  
> schemas on Rails?
> 
> Wondering what kind of standard approaches there are other than me  
> writing something like
> 
> class MyEntity \< ActiveRecord::Base  
> def active  
> return false unless active==‘y’  
> true  
> end  
> end
> 
> – Brendan Baldwin (dot com)

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 5, 2006, 1:10am UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/6 "2006-04-05T01:10:39Z")

</div>

I generated my models from postgresql tables with booleans in them.  
They  
seem to work fine without any extra effort.

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 5, 2006, 3:26pm UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/7 "2006-04-05T15:26:55Z")

</div>

Each of the database adapters is responsible for determining how to  
handle a boolean column in a way that makes sense for their specific  
backend. In the case of PostgreSQL and OpenBase this is trivial,  
since these database backends have a boolean type column built-in.  
For MySQL - as has been pointed out - booleans are usually handled as  
tinyint. The MySQL adapter therefore will default to handle tinyint  
columns as booleans. However, this behavior can be changed by  
setting MysqlAdapter.emulate\_booleans = false.

If you define your schema within rails and give the column a type  
of :boolean, then the adapter you are using will create a column of  
whatever type makes sense for that database, thus achieving (at least  
to this extent) database agnosticism. Beautiful. Sorta brings a  
tear to your eye, doesn’t it?

-Derrick S.

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 7, 2006, 12:28am UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/8 "2006-04-07T00:28:26Z")

</div>

Thats great news about MySQL and TinyInt 🙂 I didn’t realize that one.

Thanks for the replies everyone! I think I’m going to start exploring  
Migrations now.

–Brendan

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 7, 2006, 1:32am UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/9 "2006-04-07T01:32:16Z")

</div>

I don’t think this was mentioned yet. You can place a question mark  
after the getter method to inform ActiveRecord that you expect the  
attribute to be a boolean. For example:

entity = MyEntity.find(:first)  
if entity.active?

# do something

end

This is good for a couple reasons:

1. It follows the ruby standard of placing a question mark after a  
method which returns a boolean.
2. It converts the column value into a boolean. Example: ‘0’, ‘f’,  
‘false’, and ‘’ (empty string) all return false. Everything else  
returns true. Unfortunately, it doesn’t recognize “n” as false.

Hope that helps.

Ryan

---

<div class="post-metadata">

**Author:** ![Brendan\_Baldwin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/b3f665/32.png) [@Brendan\_Baldwin](https://www.ruby-forum.com/u/Brendan_Baldwin)\
**Post date:** [April 7, 2006, 9:26am UTC](https://www.ruby-forum.com/t/does-activerecord-have-support-for-boolean-columns/56849/10 "2006-04-07T09:26:59Z")

</div>

On Apr 7, 2006, at 1:29, Ryan B. wrote:

> I don’t think this was mentioned yet. You can place a question mark  
> after the getter method to inform ActiveRecord that you expect the  
> attribute to be a boolean. For example:
> 
> entity = MyEntity.find(:first)  
> if entity.active?
> 
> # do something
> 
> end

In addition you can use booleans in find\_bys:

```
entity = MyEntity.find_all_by_active(true) # no SQL here

```

Since that’s a little tricky I try to avoid hard-coding any boolean-  
like literal as one would do with :conditions.

– fxn
