Forum: Ruby hash of hashes group_by

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
428f1d76da99d9602d57b046841df29a?d=identicon&s=25 Dave Smith (railsnoob)
on 2009-06-05 15:44
Hi Guys,

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

def get_stats
  sql = "select id, value from table"
  my_data = self.class.connection.select_all(sql)
  my_data.group_by { |row| row[:id] }

the data is clearly in there as a puts shows


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?

753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (candlerb)
on 2009-06-05 17:06
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.
This topic is locked and can not be replied to.