hash of hashes group_by

Discussion in 'Ruby' started by Dave Smith, Jun 5, 2009.

  1. Dave Smith

    Dave Smith Guest

    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
    --
    Posted via http://www.ruby-forum.com/.
    Dave Smith, Jun 5, 2009
    #1
    1. Advertising

  2. 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.
    --
    Posted via http://www.ruby-forum.com/.
    Brian Candler, Jun 5, 2009
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Neumann

    [RCR] Enumerable#group_by

    Michael Neumann, May 25, 2004, in forum: Ruby
    Replies:
    2
    Views:
    115
  2. Scott  Gilpin
    Replies:
    2
    Views:
    210
  3. Perl Learner

    Hashes of hashes or just one hash ?

    Perl Learner, Jun 8, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    206
  4. Tim O'Donovan

    Hash of hashes, of hashes, of arrays of hashes

    Tim O'Donovan, Oct 27, 2005, in forum: Perl Misc
    Replies:
    5
    Views:
    201
  5. Replies:
    3
    Views:
    198
Loading...

Share This Page