Hi, I''m used to storing boolean fields as bit fields. When I try this active record returns the value as a 1 or a 0. Which is perfectly sensible ... since that is the value in the DB. Is there a way for me to hint to active record that it should be a boolean field. I''m using mysql - which I am not used to. Sorry if this is a stupid question. R.
I usually use a tinyint(1) and then add a custom accessor to the model as such: def bit_field read_attribute(''bit_field'') == 1 end Then in a view I use <%=check_box ''my_model'',''bit_field'' %> and everything works out ok from a form processing standpoint. Someone recommended this approach to me on #rubyonrails and It''s been working great ever since. I''ve occasionally also added an alias to a bit_field? method for things like StoreItem#visible where the method behaves more like a predicate and I want to use it like that in code. If it''s just being used for CRUD, though, this is not neccesary. Brian On Mon, 31 Jan 2005 21:22:48 +0000, Rob Lally <ruby-TLoEj6T5Eh2QE6UZXBUcsgC/G2K4zDHf@public.gmane.org> wrote:> Hi, > > I''m used to storing boolean fields as bit fields. When I try this active record returns the value as a 1 or a 0. Which > is perfectly sensible ... since that is the value in the DB. Is there a way for me to hint to active record that it > should be a boolean field. I''m using mysql - which I am not used to. > > Sorry if this is a stupid question. > > R. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- The years ahead pick up their dark bags. They move closer. There''s a slight rise in the silence then nothing. -- (If you''re receiving this in response to mail sent to bluczkie-OM76b2Iv3yLQjUSlxSEPGw@public.gmane.org, don''t be concerned This is my new address, but mail will be forwarded here indefinitely)
This is pretty much the approach I''ve been taking so far. But rails has spoiled me by writing all of the rest of the code for me; even having to add one line now feels like failure. Thanks for your reply, R. >Brian L. wrote:> I usually use a tinyint(1) and then add a custom accessor to the model as such: > > def bit_field > read_attribute(''bit_field'') == 1 > end > > Then in a view I use > > <%=check_box ''my_model'',''bit_field'' %> and everything works out ok > from a form processing standpoint. > > Someone recommended this approach to me on #rubyonrails and It''s been > working great ever since. > > I''ve occasionally also added an alias to a bit_field? method for > things like StoreItem#visible where the method behaves more like a > predicate and I want to use it like that in code. If it''s just being > used for CRUD, though, this is not neccesary. > > Brian > > > > > > On Mon, 31 Jan 2005 21:22:48 +0000, Rob Lally <ruby-TLoEj6T5Eh2QE6UZXBUcsgC/G2K4zDHf@public.gmane.org> wrote: > >>Hi, >> >>I''m used to storing boolean fields as bit fields. When I try this active record returns the value as a 1 or a 0. Which >>is perfectly sensible ... since that is the value in the DB. Is there a way for me to hint to active record that it >>should be a boolean field. I''m using mysql - which I am not used to. >> >>Sorry if this is a stupid question. >> >>R.
On Tue, 01 Feb 2005 07:53:54 +0000, Rob Lally <ruby-TLoEj6T5Eh2QE6UZXBUcsgC/G2K4zDHf@public.gmane.org> wrote:> This is pretty much the approach I''ve been taking so far. But rails has spoiled me by writing all of the rest of the > code for me; even having to add one line now feels like failure.If you declare the column as BOOLEAN, does it work correctly? Boolean''s just an alias for tinyint(1), so your application should still work.> Thanks for your reply, > > R. > > >Brian L. wrote: > > I usually use a tinyint(1) and then add a custom accessor to the model as such: > > > > def bit_field > > read_attribute(''bit_field'') == 1 > > end > > > > Then in a view I use > > > > <%=check_box ''my_model'',''bit_field'' %> and everything works out ok > > from a form processing standpoint. > > > > Someone recommended this approach to me on #rubyonrails and It''s been > > working great ever since. > > > > I''ve occasionally also added an alias to a bit_field? method for > > things like StoreItem#visible where the method behaves more like a > > predicate and I want to use it like that in code. If it''s just being > > used for CRUD, though, this is not neccesary. > > > > Brian > > > > > > > > > > > > On Mon, 31 Jan 2005 21:22:48 +0000, Rob Lally <ruby-TLoEj6T5Eh2QE6UZXBUcsgC/G2K4zDHf@public.gmane.org> wrote: > > > >>Hi, > >> > >>I''m used to storing boolean fields as bit fields. When I try this active record returns the value as a 1 or a 0. Which > >>is perfectly sensible ... since that is the value in the DB. Is there a way for me to hint to active record that it > >>should be a boolean field. I''m using mysql - which I am not used to. > >> > >>Sorry if this is a stupid question. > >> > >>R. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Cheers Koz
''Fraid not. The attribute still gets set to the value of the field - 0/1, as opposed to true/false. Thanks though, R. >Michael Koziarski wrote: >> On Tue, 01 Feb 2005 07:53:54 +0000, Rob Lally <ruby-TLoEj6T5Eh2QE6UZXBUcsgC/G2K4zDHf@public.gmane.org> wrote: > >>This is pretty much the approach I''ve been taking so far. But rails has spoiled me by writing all of the rest of the >>code for me; even having to add one line now feels like failure. > > > If you declare the column as BOOLEAN, does it work correctly? > Boolean''s just an alias for tinyint(1), so your application should > still work.
Brian L. wrote:> I usually use a tinyint(1) and then add a custom accessor to the model as such: > > def bit_field > read_attribute(''bit_field'') == 1 > endThis should not be necessary. AR will treat the tinyint as a bool and set up a query method as such: CREATE TABLE recipes ... TINY INT(1) is_tasty DEFAULT 1, ... r = Recipe.find 1 puts "yum yum" if r.is_tasty? The query method is how you should access the boolean value, and the form helpers like check_box should understand this magically. It''s quite handy. -Scott _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Thanks that works perfectly. R. Scott Barron wrote:> Brian L. wrote: > >> I usually use a tinyint(1) and then add a custom accessor to the model >> as such: >> >> def bit_field >> read_attribute(''bit_field'') == 1 >> end > > > This should not be necessary. AR will treat the tinyint as a bool and > set up a query method as such: > > CREATE TABLE recipes ... > TINY INT(1) is_tasty DEFAULT 1, > ... > > r = Recipe.find 1 > puts "yum yum" if r.is_tasty? > > The query method is how you should access the boolean value, and the > form helpers like check_box should understand this magically. It''s > quite handy. >