A Hash problem


#1

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


#2

On Thu, Jan 15, 2009 at 10:23 AM, Valentino L. removed_email_address@domain.invalid wrote:

CMC CRS 1
}
Valentino

Posted via http://www.ruby-forum.com/.

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 T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

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


#3

On Thu, Jan 15, 2009 at 10:23 AM, Valentino L. removed_email_address@domain.invalid wrote:

CMC CRS 1
}
Valentino

Posted via http://www.ruby-forum.com/.

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 T.
http://ramblingsonrails.com
http://www.linkedin.com/in/andrewtimberlake

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