How to return a single attribute instead of all models
Usually a query returns a user model array, but I w开发者_如何学运维ant a name array. Is there some convenient way to implement this?
Here you go:
names = User.find(:all, :conditions => ['age > 10'], :select => 'name').collect {|obj| obj.name }
Here's the Ruby 1.9 + Rails 3 way to do it.
names = User.select(:name).map(&:name)
精彩评论