# Using one-dimensional arrays in select statements (noob)

**URL:** <https://www.ruby-forum.com/t/using-one-dimensional-arrays-in-select-statements-noob/49612>\
**Category:** Rails\
**Created:** [December 6, 2005, 9:32pm UTC](https://www.ruby-forum.com/t/using-one-dimensional-arrays-in-select-statements-noob/49612 "2005-12-06T21:32:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [December 6, 2005, 9:32pm UTC](https://www.ruby-forum.com/t/using-one-dimensional-arrays-in-select-statements-noob/49612/1 "2005-12-06T21:32:12Z")

</div>

I have an issue whereby I want the attributes on an object to be saved  
in the db as simple integers and the corresponding labels to be stored  
in the class.

DB Car:  
id int, name varchar(10), speed int

Model Car:  
class Car \< ActiveRecord::Base  
Speed = %w{Slow Normal Fast}  
end

View Car:

Slow Normal Fast

What is the select statement to get the options to print out like that?  
Now I know that I could set Speed to be a hash (Speed = {“slow”=\>0,  
“normal”=\>1, “fast”=\>2}) but then I would lose the ordering, which is  
important. I could use an associative array as well (Speed = [[“Slow”,  
0],[“Normal”, 1],[“Fast”, 2]]) but this Car object will have a number of  
similar attributes so that would seem to violate the DRY principal  
having to put 0, 1, 2, etc in every time.

Is there a way to print out a select statement using a simple  
one-dimensional array with the item’s index value acting as the key in  
the option tag?

I very much want to do this the Ruby way. Any help would be greatly  
appreciated.

---

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [December 7, 2005, 12:49am UTC](https://www.ruby-forum.com/t/using-one-dimensional-arrays-in-select-statements-noob/49612/2 "2005-12-07T00:49:55Z")

</div>

On 12/6/05, sean [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> What is the select statement to get the options to print out like that?  
> Now I know that I could set Speed to be a hash (Speed = {“slow”=\>0,  
> “normal”=\>1, “fast”=\>2}) but then I would lose the ordering, which is  
> important. I could use an associative array as well (Speed = [[“Slow”,  
> 0],[“Normal”, 1],[“Fast”, 2]]) but this Car object will have a number of  
> similar attributes so that would seem to violate the DRY principal  
> having to put 0, 1, 2, etc in every time.

There isn’t a method that does exactly what you want, as far as I  
know. But here’s an idea that is pretty good with regards to DRY:

class Array  
def with\_indexes  
a = []  
each\_with\_index {|x,i| a \<\< [x,i]}  
a  
end  
end

Model Car:  
class Car \< ActiveRecord::Base  
@@speeds = %w{Slow Normal Fast}  
cattr\_reader :speeds  
end

View Car:  
select(“mycar”, “speed”, Car.speeds.with\_indexes)

---

<div class="post-metadata">

**Author:** ![sean](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f0a364/32.png) [@sean](https://www.ruby-forum.com/u/sean)\
**Post date:** [December 7, 2005, 3:41pm UTC](https://www.ruby-forum.com/t/using-one-dimensional-arrays-in-select-statements-noob/49612/3 "2005-12-07T15:41:28Z")

</div>

jeremyevans0 wrote:

> There isn’t a method that does exactly what you want, as far as I  
> know. But here’s an idea that is pretty good with regards to DRY:

That is an excellent solution, thanks very much for your input on what  
was a vexing issue.
