Topic: When referencing an element in a Hash I get a TypeError

I am building an array of hashes as follows (matching the names against the first 3 letters of digits entered on an Asterisk system):

  def self.fetch_users(combinations)
    db_users = User.find(:all, :conditions => ["active =?", TRUE])
    users =
    cnt = 0
    db_users.each do |db_user|
      combinations.each do |combination|
        if combination.slice(0,3) == db_user["#{$HELPERS["liquid_directory"]["name_order"]}"].slice(0,3).downcase!
          users[cnt] = db_user.attributes
          cnt += 1
    return users

But, then when I try to reference an element of that hash here:

users.each do |user|
  log "User: " + user["first_name"]

I get this error:

#<TypeError: can't convert String into Integer>

You may see my hashes here:

Users: {0=>{"salt"=>nil, "updated_at"=>Wed May 23 14:42:46 +0200 2007, "crypted_password"=>nil, "username"=>"johndoe", "remember_token_expires_at"=>nil, "id"=>2, "remember_token"=>nil, "first_name"=>"John", "last_name"=>"Doe", "email"=>"", "created_at"=>Wed May 23 14:42:46 +0200 2007, "active"=>true}, 1=>{"salt"=>nil, "updated_at"=>Wed May 23 15:42:58 +0200 2007, "crypted_password"=>nil, "username"=>"janedoe", "remember_token_expires_at"=>nil, "id"=>3, "remember_token"=>nil, "first_name"=>"Jane", "last_name"=>"Doe", "email"=>"", "created_at"=>Wed May 23 15:42:58 +0200 2007, "active"=>true}}

What am I doing wrong here to make Ruby think I am attempting to convert a string into an integer?

Last edited by muppetmaster (2007-05-23 11:24:03)

Re: When referencing an element in a Hash I get a TypeError

Changed the way I am iterating to a traditional while loop:

while user_cnt <= users.length do

And now it works fine.  What does the users.each do that I am not understanding?