How to get array with clear data from Active Record query without using map
Hi I would like to ask if its possible get array with clear data from ActiveRecord query without using map, collect or each.
names = User.find(:all, :select => "name")
return names == [#<User name:"Peter">,#<User name:"Martin">]
and I want names开发者_运维技巧 == ["Peter", "Martin"] without using map, collect or each. Thanks for your answers.
User.connection.select_values("SELECT name FROM users")
#=> ["francois"]
Even if ActiveRecord provided a method to do what you want (single column to array of values), it would internally use a loop (each or collect) to do it.
I don't see what is so wrong with using a loop in this case, Ruby makes it quite easy to do it.
users = User.find(:all, :select => "name").collect { |u| u.name }
No you can't do that w/o using map, collect each ... etc......
In otherwords you can't get result like this in a single query.
精彩评论