Forum: Ruby on Rails Boolean attributes - not so simple as I thought

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
D63d38971d813a9c7cf06da46832b8f8?d=identicon&s=25 r00by n00by (Guest)
on 2006-05-11 00:41
Suppose I have a table 'items' with the attribute 'bool' which is
There's an array @attribs which holds :bool and some other symbols of
attributes which are not boolean.

class Item < ActiveRecord::Base

  def attribs
    [:bool , :x , :y]

The trouble is that @item.bool? and @item.bool behave differently.
If I set @item.bool=5 then
 @item.bool == 5
 @item.bool? == true
Worse than that, if I set @item.bool='a string'
 @item.bool == false
 @item.bool? == true
This is confusing and may cause problems. I think the value of the
attribute bool should always be accessed by bool? , but I need to get
and set the value of the attributes in @attribs like this:
    get: @item.attribs.each {|a| puts @item.send(a)}
    set: @item.attribs.each {|a| @item.send(a.to_s + '=', some_hash[a])}
in the get part the method that gets called by send is @item.bool and
not @item.bool?.

Another issue is validation: should I use
    validates_inclusion_of :bool? , :in=>[false,true]
    validates_inclusion_of :bool , :in=>[false,true]
    validates_inclusion_of :bool , :in=>0..1
or maybe something else?

Can you please help me with these issues?

I am using Ruby 1.8.2 + Rails 1.0 + MySQL.
This topic is locked and can not be replied to.