# Can i use a hash on a collection\_select? If don't, alternatives

**URL:** <https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245>\
**Category:** Rails\
**Created:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245 "2010-06-17T16:22:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Miguel.camba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@Miguel.camba](https://www.ruby-forum.com/u/Miguel.camba)\
**Post date:** [June 17, 2010, 4:22pm UTC](https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245/1 "2010-06-17T16:22:29Z")

</div>

In my current application, i have food categories: mexican, home made,  
japanese, ect  
As these categories only have ID and name, no other attributes, i dont  
want to create a table on the DB.  
My first idea was to create a constant named FOOD\_CATEGORIES, that is  
has like:  
FOOD\_CATEGORIES = { 1=\>‘mexican’,  
2=\>‘japanese’, … }  
BUT, since the hash doesn’t have :id method, i dont know how to use  
this constant on a collection\_select in my views.

Do you have a better idea?

Thanks

---

<div class="post-metadata">

**Author:** ![Miguel.camba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@Miguel.camba](https://www.ruby-forum.com/u/Miguel.camba)\
**Post date:** [June 17, 2010, 4:26pm UTC](https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245/2 "2010-06-17T16:26:21Z")

</div>

On 17 June 2010 12:17, Miguel.camba [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Do you have a better idea?

Maybe options\_for\_select is what you are looking for.

Colin

---

<div class="post-metadata">

**Author:** ![Miguel.camba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@Miguel.camba](https://www.ruby-forum.com/u/Miguel.camba)\
**Post date:** [June 17, 2010, 5:41pm UTC](https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245/3 "2010-06-17T17:41:37Z")

</div>

I dont think so. I want to show a select that shows que values of the  
hash but the stored data when you submit a form is the key of that  
value un the hash.

---

<div class="post-metadata">

**Author:** ![Miguel.camba](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/85e7bf/32.png) [@Miguel.camba](https://www.ruby-forum.com/u/Miguel.camba)\
**Post date:** [June 17, 2010, 6:18pm UTC](https://www.ruby-forum.com/t/can-i-use-a-hash-on-a-collection-select-if-dont-alternatives/190245/4 "2010-06-17T18:18:31Z")

</div>

I have just found a solution, but using a helper  
In application\_helper i wrote this function:  
def select\_from\_hash(object, name, arg)  
select = “\<select id=”#{object}\_#{name}" name="#{object}  
[#{name}]""  
arg.each do |i,e|  
select += “\<option value=”#{i}"\>#{e}"  
end  
select += “”  
end  
And in my views I use it this way (im using Haml):  
.field= render :inline=\> select\_from\_hash(:profile, :loved\_food,  
CATEGORIES)

Works fine but im all ears for other-more-elegant approach.
