Brendan Baldwin
2006-Apr-04 20:30 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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)
doug livesey
2006-Apr-04 21:08 UTC
[Rails] Re: Does ActiveRecord have support for "Boolean" columns?
> 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... -- Posted via http://www.ruby-forum.com/.
Paul Barry
2006-Apr-04 21:09 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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 4/4/06, Brendan Baldwin <brendan.baldwin@gmail.com> 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) > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/3facd224/attachment-0001.html
doug livesey
2006-Apr-04 21:15 UTC
[Rails] Re: Does ActiveRecord have support for "Boolean" columns?
> 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! -- Posted via http://www.ruby-forum.com/.
Steve Koppelman
2006-Apr-04 21:16 UTC
[Rails] Re: Does ActiveRecord have support for "Boolean" columns?
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)-- Posted via http://www.ruby-forum.com/.
Larry White
2006-Apr-04 23:09 UTC
[Rails] Re: Does ActiveRecord have support for "Boolean" columns?
I generated my models from postgresql tables with booleans in them. They seem to work fine without any extra effort.> > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060404/88ba4dd6/attachment.html
Derrick Spell
2006-Apr-05 13:24 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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 On Apr 4, 2006, at 4:30 PM, 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) > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Brendan Baldwin
2006-Apr-06 22:25 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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 4/5/06, Derrick Spell <derrickspell@cdmplus.com> wrote:> 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 > > > On Apr 4, 2006, at 4:30 PM, 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) > > _______________________________________________ > > Rails mailing list > > Rails@lists.rubyonrails.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Ryan Bates
2006-Apr-06 23:29 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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 Apr 6, 2006, at 3:25 PM, Brendan Baldwin wrote:> 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 4/5/06, Derrick Spell <derrickspell@cdmplus.com> wrote: >> 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 >> >> >> On Apr 4, 2006, at 4:30 PM, 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) >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails
Xavier Noria
2006-Apr-07 07:24 UTC
[Rails] Does ActiveRecord have support for "Boolean" columns?
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 > endIn 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