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)
on 2006-04-05 00:31
on 2006-04-05 01: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...
on 2006-04-05 01:11
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.
on 2006-04-05 01: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!
on 2006-04-05 01: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)
on 2006-04-05 03:10
I generated my models from postgresql tables with booleans in them. They seem to work fine without any extra effort.
on 2006-04-05 17:26
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.
on 2006-04-07 02:28
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
on 2006-04-07 03:32
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
on 2006-04-07 11:26
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