# How to store contents of table in Hash?

**URL:** <https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542>\
**Category:** Rails\
**Created:** [March 11, 2009, 6:48am UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542 "2009-03-11T06:48:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![VrishaliSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VrishaliSS](https://www.ruby-forum.com/u/VrishaliSS)\
**Post date:** [March 11, 2009, 6:48am UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542/1 "2009-03-11T06:48:53Z")

</div>

Hi evrybody…

I was storing the values in hash using…

places = [  
{:address=\>‘555 Irving, San Francisco,  
CA’,:description=\>‘Irving’,:image=\>’/images/1.jpg’},  
{:address=\>‘1401 Valencia St, San Francisco,  
CA’,:description=\>‘Valencia’,:image=\>’/images/2.jpg’},  
]

But now I want to take these values from database…I created ,  
migrated table correctly…

N now using the statement  
@companies = Company.find(:all)

But now i dont understand how can i store these fetched values in hash-

> ‘places’

I dont want static values…

So can anybody please help me for it…  
Thanks…

---

<div class="post-metadata">

**Author:** ![VrishaliSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VrishaliSS](https://www.ruby-forum.com/u/VrishaliSS)\
**Post date:** [March 11, 2009, 7:13am UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542/2 "2009-03-11T07:13:56Z")

</div>

Vrishali… wrote:

> N now using the statement  
> @companies = Company.find(:all)
> 
> But now i dont understand how can i store these fetched values in hash-
> 
> > ‘places’
> 
> I dont want static values…

I’m not quite sure why you need to store the values from the  
ActiveRecords in a hash, but basically ActiveRecord uses a hash, or  
something like a hash, internally and it’s easy to get to that using the  
attributes method:

places = []  
@companies.each do |company|  
places \<\< company.attributes  
end

This will build an array of hashes just like what you had with the  
static values. But, like I said I don’t know why you would need to do  
this. You could just use the ActiveRecords directly. And if you wanted a  
serialized version of them for some reason then JSON is a good format  
for that:

@companies.to\_json

---

<div class="post-metadata">

**Author:** ![VrishaliSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VrishaliSS](https://www.ruby-forum.com/u/VrishaliSS)\
**Post date:** [March 13, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542/3 "2009-03-13T12:29:03Z")

</div>

Thanks for the help…

actually i am using places hash evrywhere…so i dnt want  
modifications throughout the code…n files…so…i want to put the  
all retrieved records to be placed in that…places.  
n i want it at the start of controller call…

thats y…

ok i tried ur statments given…but still it is giving error…

TypeError in GmapController#index

can’t convert nil into String

RAILS\_ROOT: /home/ubuntu/gmaptry  
Application Trace | Framework Trace | Full Trace

app/controllers/gmap\_controller.rb:57:in `+' app/controllers/gmap_controller.rb:57:in`get\_geocode’  
app/controllers/gmap\_controller.rb:30:in `index' app/controllers/gmap_controller.rb:28:in`each’  
app/controllers/gmap\_controller.rb:28:in `each_with_index' app/controllers/gmap_controller.rb:28:in`index’

…

so what should i do…please help…

also i am using evrywhere…  
place[:address]  
place[:description]…

so how can i specify that…

please help me…

thanks…

---

<div class="post-metadata">

**Author:** ![VrishaliSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VrishaliSS](https://www.ruby-forum.com/u/VrishaliSS)\
**Post date:** [March 13, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542/4 "2009-03-13T12:29:38Z")

</div>

Thanks for the help…

actually i am using places hash evrywhere…so i dnt want  
modifications throughout the code…n files…so…i want to put the  
all retrieved records to be placed in that…places.  
n i want it at the start of controller call…

thats y…

ok i tried ur statments given…but still it is giving error…

TypeError in GmapController#index

can’t convert nil into String

RAILS\_ROOT: /home/ubuntu/gmaptry  
Application Trace | Framework Trace | Full Trace

app/controllers/gmap\_controller.rb:57:in `+' app/controllers/gmap_controller.rb:57:in`get\_geocode’  
app/controllers/gmap\_controller.rb:30:in `index' app/controllers/gmap_controller.rb:28:in`each’  
app/controllers/gmap\_controller.rb:28:in `each_with_index' app/controllers/gmap_controller.rb:28:in`index’

…

so what should i do…please help…

also i am using evrywhere…  
place[:address]  
place[:description]…

so how can i specify that…

please help me…

thanks…

---

<div class="post-metadata">

**Author:** ![VrishaliSS](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/7ab992/32.png) [@VrishaliSS](https://www.ruby-forum.com/u/VrishaliSS)\
**Post date:** [March 13, 2009, 1:49pm UTC](https://www.ruby-forum.com/t/how-to-store-contents-of-table-in-hash/162542/5 "2009-03-13T13:49:27Z")

</div>

ok…

I got the solution by myself

now i am using the code…

@companies.each do |company|  
places [ {:address =\> company.addr, :description =\>  
company.cname , :image =\> company.img}]  
end

it is working without ant error…

but now the problem is that …

the values are getting overwritted in places array…and only last  
record is saved…

so what can i do for increamenting places index…

or anybody knows any another method…

please help me…
