# Hash of hashes group\_by

**URL:** <https://www.ruby-forum.com/t/hash-of-hashes-group-by/169578>\
**Category:** Ruby\
**Created:** [June 5, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/hash-of-hashes-group-by/169578 "2009-06-05T15:44:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [June 5, 2009, 3:44pm UTC](https://www.ruby-forum.com/t/hash-of-hashes-group-by/169578/1 "2009-06-05T15:44:12Z")

</div>

Hi Guys,

I have the following code which grabs some data from the database in my  
model

def get\_stats  
sql = “select id, value from table”  
my\_data = self.class.connection.select\_all(sql)  
my\_data.group\_by { |row| row[:id] }  
end

the data is clearly in there as a puts shows

value4id1  
value1id2

but when trying to access the values

x = my\_data[1][:value]

The error occurred while evaluating nil.[]

Can anyone shed some light on this?

Cheers

---

<div class="post-metadata">

**Author:** ![dnstevenson](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8dc957/32.png) [@dnstevenson](https://www.ruby-forum.com/u/dnstevenson)\
**Post date:** [June 5, 2009, 5:06pm UTC](https://www.ruby-forum.com/t/hash-of-hashes-group-by/169578/2 "2009-06-05T17:06:03Z")

</div>

Try ‘p xxx’ (or ‘puts xxx.inspect’) instead of ‘puts xxx’.

This will show you things like the difference between a string and a  
number, or between an empty string and nil.
