Forum: Ruby on Rails Does ActiveRecord have support for "Boolean" columns?

944973814a5fb4df6912106ff16074a0?d=identicon&s=25 Brendan Baldwin (Guest)
on 2006-04-04 22:31
(Received via mailing list)
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)
2837408ba0375c40b235f0a5a5107d37?d=identicon&s=25 Doug Livesey (biot023)
on 2006-04-04 23:08
> 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...
6661ef9d747db3af8896cd94959d717d?d=identicon&s=25 Paul Barry (Guest)
on 2006-04-04 23:11
(Received via mailing list)
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.
2837408ba0375c40b235f0a5a5107d37?d=identicon&s=25 Doug Livesey (biot023)
on 2006-04-04 23:15
> 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!
34f5b045aec62235c17458650ea75353?d=identicon&s=25 Steve Koppelman (hatless)
on 2006-04-04 23:16
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)
D4b246038154d7cc2363256bd25a4fe0?d=identicon&s=25 Larry White (Guest)
on 2006-04-05 01:10
(Received via mailing list)
I generated my models from postgresql tables with booleans in them.
They
seem to work fine without any extra effort.
Bc80625db60e9db4394c51d6c1892b49?d=identicon&s=25 Derrick Spell (Guest)
on 2006-04-05 15:26
(Received via mailing list)
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 Spell
944973814a5fb4df6912106ff16074a0?d=identicon&s=25 Brendan Baldwin (Guest)
on 2006-04-07 00:28
(Received via mailing list)
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
085541f9546d0505433183b5f95bbf62?d=identicon&s=25 Ryan Bates (Guest)
on 2006-04-07 01:32
(Received via mailing list)
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
7223c62b7310e164eb79c740188abbda?d=identicon&s=25 Xavier Noria (Guest)
on 2006-04-07 09:26
(Received via mailing list)
On Apr 7, 2006, at 1:29, Ryan Bates 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.