# Valid options from hash

**URL:** <https://www.ruby-forum.com/t/valid-options-from-hash/119414>\
**Category:** Rails\
**Created:** [November 6, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/valid-options-from-hash/119414 "2007-11-06T20:40:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [November 6, 2007, 8:40pm UTC](https://www.ruby-forum.com/t/valid-options-from-hash/119414/1 "2007-11-06T20:40:58Z")

</div>

Which is the best way to create a thin key/value related to some  
model? where a field only could insert those values (keys as 0,1) and  
where could be showed the values (as Red, Blue) from a drop-list

COLOUR\_OPTIONS = {0=\>“Red”, 1=\>“Blue”}

t.string :colour

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [November 7, 2007, 1:42am UTC](https://www.ruby-forum.com/t/valid-options-from-hash/119414/2 "2007-11-07T01:42:36Z")

</div>

On Nov 6, 2007 2:40 PM, Kless [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Which is the best way to create a thin key/value related to some  
> model? where a field only could insert those values (keys as 0,1) and  
> where could be showed the values (as Red, Blue) from a drop-list
> 
> COLOUR\_OPTIONS = {0=\>“Red”, 1=\>“Blue”}
> 
> t.string :colour

validates\_inclusion\_of :colour, :in =\> COLOUR\_OPTIONS.keys

That might give you some ideas for starters.

I will say that I would prefer to just store the actual values (“Red”,  
“Blue”, etc.) or if you want to store numeric keys, then create an  
actual  
table and setup an association.

class Whatever \< ActiveRecord::Base  
belongs\_to :colour  
end

class Colour \< ActiveRecord::Base  
has\_many :whatevers  
end

HTH,  
Michael G.

---

<div class="post-metadata">

**Author:** ![Kless](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/cc9497/32.png) [@Kless](https://www.ruby-forum.com/u/Kless)\
**Post date:** [November 7, 2007, 10:31am UTC](https://www.ruby-forum.com/t/valid-options-from-hash/119414/3 "2007-11-07T10:31:47Z")

</div>

Thanks. I’ll use tables.
