From: Andrew Timberlake Date: 2009-01-15T17:59:34+09:00 Subject: Re: A Hash problem ------=_Part_16074_9519588.1232010033480 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On Thu, Jan 15, 2009 at 10:23 AM, Valentino Lun wrote: > 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 > -- > 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 Timberlake http://ramblingsonrails.com http://www.linkedin.com/in/andrewtimberlake "I have never let my schooling interfere with my education" - Mark Twain ------=_Part_16074_9519588.1232010033480--