Forum: Ruby on Rails Best way to format attributes during visualization/editing

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.
E1df36bbce4853d90356eb9fc95ed433?d=identicon&s=25 Carmine M. (carminem)
on 2006-06-13 15:16
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:

<p><label for="grain_kind">Select Kind</label><br/>
<%= 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:
<p><label for="grain_kind">Select Kind</label><br/>
<%= 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).
This topic is locked and can not be replied to.