Suppose I have a table ‘items’ with the attribute ‘bool’ which is
boolean.
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]
end
}
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]
or
validates_inclusion_of :bool , :in=>[false,true]
or
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.