[Rails] Boolean values

Michael Koziarski koziarski at gmail.com
Tue Feb 1 09:25:22 GMT 2005


On Tue, 01 Feb 2005 07:53:54 +0000, Rob Lally <ruby at roblally.plus.com> 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 at roblally.plus.com> 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 at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
> 


-- 
Cheers

Koz


More information about the Rails mailing list