Getting the value from an array in the model in rails

Posted by slythic on Stack Overflow See other posts from Stack Overflow or by slythic
Published on 2010-05-22T20:22:21Z Indexed on 2010/05/22 20:30 UTC
Read the original article Hit count: 203

Filed under:
|
|

Hi all,

I have a relatively simple problem. I have a model named Item which I've added a status field. The status field will only have two options (Lost or Found). So I created the following array in my Item model:

STATUS = [ [1, "Lost"], [2, "Found"]]

In my form view I added the following code which works great:

<%= collection_select :item, :status, Item::STATUS, :first, :last, {:include_blank => 'Select status'}  %>

This stores the numeric id (1 or 2) of the status in the database. However, in my show view I can't figure out how to convert from the numeric id (again, 1 or 2) to the text equivalent of Lost or Found.

Any ideas on how to get this to work? Is there a better way to go about this?

Many thanks, Tony

© Stack Overflow or respective owner

Related posts about arrays

Related posts about model