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

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
accessor as follows:

def kind()

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()

def kind_id=(value)

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.


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).