# A Hash problem

**URL:** <https://www.ruby-forum.com/t/a-hash-problem/157781>\
**Category:** Ruby\
**Created:** [January 15, 2009, 9:24am UTC](https://www.ruby-forum.com/t/a-hash-problem/157781 "2009-01-15T09:24:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 9:24am UTC](https://www.ruby-forum.com/t/a-hash-problem/157781/1 "2009-01-15T09:24:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 10:01am UTC](https://www.ruby-forum.com/t/a-hash-problem/157781/2 "2009-01-15T10:01:34Z")

</div>

On Thu, Jan 15, 2009 at 10:23 AM, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## CMC CRS 1 } Valentino
> 
> Posted via [http://www.ruby-forum.com/](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://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

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

---

<div class="post-metadata">

**Author:** ![on9west](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/o/6f9a4e/32.png) [@on9west](https://www.ruby-forum.com/u/on9west)\
**Post date:** [January 15, 2009, 10:03am UTC](https://www.ruby-forum.com/t/a-hash-problem/157781/3 "2009-01-15T10:03:20Z")

</div>

On Thu, Jan 15, 2009 at 10:23 AM, Valentino L. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## CMC CRS 1 } Valentino
> 
> Posted via [http://www.ruby-forum.com/](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://ramblingsonrails.com)  
[http://www.linkedin.com/in/andrewtimberlake](http://www.linkedin.com/in/andrewtimberlake)

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