# Making a hash from AR data

**URL:** <https://www.ruby-forum.com/t/making-a-hash-from-ar-data/120344>\
**Category:** Rails\
**Created:** [November 15, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/making-a-hash-from-ar-data/120344 "2007-11-15T23:32:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 15, 2007, 11:32pm UTC](https://www.ruby-forum.com/t/making-a-hash-from-ar-data/120344/1 "2007-11-15T23:32:05Z")

</div>

I want to routinely make updated hashes in controller methods and use  
the data in views. Can I put this in a method and be done?

my\_hash = {}  
widget\_holder = Cars.find(:all)  
my\_hash = widget\_holder.map { |widget| widget.parking\_spot\_number,  
widget.name}

Is that it? Is an array of two element arrays a hash or is there  
another step?  
I know I’ve seen this done but i cant find it in my books or via search.

Can I then go:

\<%=h my\_hash[36] %\> in a table slot and get it to return "  
Blue Cadillac Eldorado"  
if the instance of Car had values for name(Blue Cadillac Eldorado) and  
parking  
spot(36) accordingly?

Am I missing a step? (or two?)

---

<div class="post-metadata">

**Author:** ![tomn](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/c89c15/32.png) [@tomn](https://www.ruby-forum.com/u/tomn)\
**Post date:** [November 16, 2007, 3:33am UTC](https://www.ruby-forum.com/t/making-a-hash-from-ar-data/120344/2 "2007-11-16T03:33:27Z")

</div>

Ok I stunk that one up and to boot, the question had recently been  
answered. (sorry i didnt look hard enough)

Here is the link to the answer(elsewhere on this forum)

[http://www.ruby-forum.com/topic/129491#new](http://www.ruby-forum.com/topic/129491#new)

While what I put forward didn’t even return an array, I am curious  
about one issue.

Is an array of strings that looks like a hash a hash? Could you go

x = value + " =\> " + value  
@existing\_hash \<\< x

If so, could i use map like this?  
new\_hash = object\_collection.map{|object| object.key\_attribute + " =\>" +  
object.value\_attribute }
