开发者

Return ActiveRecord query as variable, not hash?

I am doing an ActiveRecord find on a model as such

@foo = MyModel.find(:all, :select => 'year')

As you can see, I only need the year column from this, so my ideal output would be

["2008", "2009", "2010"]

Instead, though, I get an a hash of the models, with each one containing the year, as such:

[#<MyModel year: "2008">, #<MyModel year: "2009">, #<MyModel year: "2010">]

I can loop through it as such to convert it to my ideal output:

@years = []
for bar in @foo
    @years.push(bar.year)
end

but is there a way to retrieve this result to begin with? (i.e. without going through the extra p开发者_如何学编程rocessing?). If there is not, what is a more concise way to do this processing?

Thank you.


try:

@foo = MyModel.find(:all, :select => 'year').map(&:year)

You also can call .uniq() on the result to remove duplicates.

@foo = MyModel.find(:all, :select => 'year').map(&:year).uniq

This is the short form for

@foo = MyModel.find(:all, :select => 'year').map{ |model| model.year }

This loops over the MyModel Array and crates a new array of the return values with in the block (model.year).

or even shorter code, but first calling everything from db:

@foo = MyModel.all.map(&:year)


If you don't want to instantiate models, you'd do it this way:

MyModel.connection.select_values("SELECT year FROM my_models")

This will return an Array of String objects. Then you can transform using Ruby tools as you see fit.


You can use map to change every value in an array. Example:

@foo = MyModel.find(:all, :select => 'year').map { |model_object| model_object.year }
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜