开发者

How to return a single attribute instead of all models

开发者 https://www.devze.com 2023-01-23 16:54 出处:网络
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:

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)
0

精彩评论

暂无评论...
验证码 换一张
取 消