A Hash problem

V

Valentino Lun

Dear all

I want to build a hash which store the values from database, but failed.
Please give me some hints please. Thank you.

services table
h_code s_code status
AHN BBS 3
AHN MBS 1
AHN HMS 1
CMC CRS 1
CMC CPS 1
CMC BTNS 1
GH CRS 1
GH CPS 1


My expectation
all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
"CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
"GH" => {"CRS" = 1, "CPS" => 1}
}


all = {}
Service.find:)all).each do |s|
# How to do here?
# I try all[s.h_code][s.s_code]=s.status, but not work
end

Thank you very much

Valentino
 
A

Andrew Timberlake

[Note: parts of this message were removed to make it a legal post.]

Dear all

I want to build a hash which store the values from database, but failed.
Please give me some hints please. Thank you.

services table
h_code s_code status
AHN BBS 3
AHN MBS 1
AHN HMS 1
CMC CRS 1
CMC CPS 1
CMC BTNS 1
GH CRS 1
GH CPS 1


My expectation
all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
"CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
"GH" => {"CRS" = 1, "CPS" => 1}
}


all = {}
Service.find:)all).each do |s|
# How to do here?
# I try all[s.h_code][s.s_code]=s.status, but not work
end

Thank you very much

Valentino
Try the following:

#Create a new hash where every key has a default entry of an empty hash
(hash of hashes)
all = Hash.new{|h,k| h[k] = {}}
Service.find:)all).each do |s|

end
h_code s_code status

--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
 
A

Andrew Timberlake

[Note: parts of this message were removed to make it a legal post.]

Dear all

I want to build a hash which store the values from database, but failed.
Please give me some hints please. Thank you.

services table
h_code s_code status
AHN BBS 3
AHN MBS 1
AHN HMS 1
CMC CRS 1
CMC CPS 1
CMC BTNS 1
GH CRS 1
GH CPS 1


My expectation
all = {"AHN" => {"BBS" => 3, "MBS" => 1, "HMS" => 1}
"CMC" => {"CRS" => 1, "CPS" => 1, "BTNS" => 1}
"GH" => {"CRS" = 1, "CPS" => 1}
}


all = {}
Service.find:)all).each do |s|
# How to do here?
# I try all[s.h_code][s.s_code]=s.status, but not work
end

Thank you very much

Valentino
Sorry about the previous post - I clicked ENTER too soon

Try the following:

#Create a new hash where every key has a default entry of an empty hash
# (hash of hashes)
all = Hash.new{|h,k| h[k] = {}}

Service.find:)all).each do |s|
all[s.h_code][s.s_code] = status
end

--
Andrew Timberlake
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

"I have never let my schooling interfere with my education" - Mark Twain
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,251
Latest member
41Ki

Latest Threads

Top