0

I discovered this bug while reproducing Michael Hartl's scaffolded rails demo_app from his rails tutorial.

I went into the rails console

rails c

I asked for the User.all array of hashes and out it through its paces:

2.0.0-p0 :012 > User.all

  User Load (0.3ms)  SELECT "users".* FROM "users" 
 => [#<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]

2.0.0-p0 :013 > User.all[0]

  User Load (0.2ms)  SELECT "users".* FROM "users" 
 => #<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06"> 

2.0.0-p0 :015 > User.all[0]['id']

  User Load (0.2ms)  SELECT "users".* FROM "users" 
 => 2 

So far, so good.

However, User.all does not respond to the command to list say just the id's or names of all users:

2.0.0-p0 :017 > User.all { |i| puts i['id'] }

  User Load (0.2ms)  SELECT "users".* FROM "users" 
 => [#<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06"gt;] 

2.0.0-p0 :019 >User.all{ |i| puts i['name'] }

  User Load (0.3ms)  SELECT "users".* FROM "users" 
 => [#<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">] 

Assigning an arbitrary variable a to the array of hashes User.all resolves the issue:

2.0.0-p0 :021 >a.each {|i| puts i['id'] }

2
 => [#<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">]

2.0.0-p0 :022 >a.each {|i| puts i['name'] }

Lisa Johnson
 => [#<User id: 2, name: "Lisa Johnson", email: "ljohnson@yahoo.com", created_at: "2013-04-02 03:30:06", updated_at: "2013-04-02 03:30:06">] 

This User.all issue affects at least ruby versions 1.9.2,1.9.3 and 2.0.0. Whoever is responsible for writing the gem that created User.all needs to go over his all method. For whatever it's worth, I am working with rails 3.2.12

Vietnhi Phuvan
  • 2,704
  • 2
  • 25
  • 25

1 Answers1

0

Nothing to discuss - its a straightforward bug report. If there is a question, the question would be "why is User.all is not behaving like the array of hashes it is?"

Vietnhi Phuvan
  • 2,704
  • 2
  • 25
  • 25