Best way to format attributes during visualization/editing

Hi to everyone,

My name’s Carmine and I’m a rails newbie. So, here’s my newbie question
:slight_smile:

I have a model with one attribute named “kind” of type integer.
This attribute can assume values within the range [0…5].
Each value has a “verbose” representation e.g.: 0 => grain, 1 => liquid

To visualize the verbose representation I’ve overridden the default
attribute’s
accessor as follows:

def kind()
VERBOSE.index(read_attribute(:kind))
end

This works like a charm.

Now in my editing/inserting view i have this:

Select Kind
<%= select("grain", "kind", Grain::KINDS, { :include_blank => true}) %>

This piece of code doesn’t work because it tries to call the “kind”
method of the “grain” object. Normally it expects an integer while now
it returns a string.

So, I have modified the snippet above as follows:

Select Kind
<%= select("grain", "kind_id", Grain::KINDS, { :include_blank => true}) %>

This has forced me to define other two accessors in the model in order
to solve the problem:

def kind_id()
read_attribute(:kind)
end

def kind_id=(value)
write_attribute(:kind,value)
end

Now, the question is: Is this a correct way to do this stuff or there’s
something better I could do?

Thanks in advance for your help.
Regards,
Carmine

P.S.:

Of course I could change the attribute’s original type from integer to
string to
simplify everything (and probably is something I’m going to do).