开发者

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜