How to store contents of table in Hash?

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…

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

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:inget_geocode’
app/controllers/gmap_controller.rb:30:in index' app/controllers/gmap_controller.rb:28:ineach’
app/controllers/gmap_controller.rb:28:in each_with_index' app/controllers/gmap_controller.rb:28:inindex’

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…

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:inget_geocode’
app/controllers/gmap_controller.rb:30:in index' app/controllers/gmap_controller.rb:28:ineach’
app/controllers/gmap_controller.rb:28:in each_with_index' app/controllers/gmap_controller.rb:28:inindex’

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…

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 forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs