Forum: Ruby on Rails How to store contents of table in Hash???

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
4c9dbbcecdb826fb1443f222850644a0?d=identicon&s=25 Vrishali.... (Guest)
on 2009-03-11 06:48
(Received via mailing list)
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...
280b78a61a968391b7e07e912be102a8?d=identicon&s=25 Robert Walker (robert4723)
on 2009-03-11 07:13
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
4c9dbbcecdb826fb1443f222850644a0?d=identicon&s=25 Vrishali.... (Guest)
on 2009-03-13 12:29
(Received via mailing list)
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...
4c9dbbcecdb826fb1443f222850644a0?d=identicon&s=25 Vrishali.... (Guest)
on 2009-03-13 12:29
(Received via mailing list)
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...
4c9dbbcecdb826fb1443f222850644a0?d=identicon&s=25 Vrishali.... (Guest)
on 2009-03-13 13:49
(Received via mailing list)
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....
This topic is locked and can not be replied to.