Details here: http://stackoverflow.com/questions/11784727/mongoid-3-access-map-reduce-results
The problem I have at the moment, in summary, is:
mr_collection = self.where(query).map_reduce(map, reduce).finalize(finalize).out({:replace => 'mr_results'})
limit = (options[:limit] || 10)
skip = (options[:skip].to_i || nil)
page = if skip >= limit
((skip+limit) / limit)
else
1
end
sort = if options[:sort_by_vintage]
[['value.vy', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
elsif options[:sort_by_sDate]
[['value.sDate', :desc], ['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
else
[['value.s', (options[:sort] || :desc)], ['value.pml', :asc]]
end
paginator = WillPaginate::Collection.new(page, limit, collection_count)
collection = mr_collection.find({},{
:sort => sort,
:limit => limit,
:skip => skip
}
)
Calling collection.to_a raises undefined method `call' for #<Hash:
This must be an issue with Moped::BSON::Document, because this works fine:
1.9.3p194 :007 > y = [{y: 20}, {z: 24}, {y: 100}].each
=> #<Enumerator: [{:y=>20}, {:z=>24}, {:y=>100}]:each>
1.9.3p194 :008 > y.to_a
=> [{:y=>20}, {:z=>24}, {:y=>100}]
But:
(rdb:1) collection
#<Enumerator: [{"_id"=>4545.0, "value"=>{"s"=>4.1, "p"=>14.25, "pml"=>0.019}}, {"_id"=>4555.0, "value"=>{"s"=>3.9, "p"=>13.95, "pml"=>0.0186}}, {"_id"=>4558.0, "value"=>{"s"=>4.1, "p"=>17.95, "pml"=>0.02393}}, {"_id"=>4788.0, "value"=>{"s"=>4.2, "p"=>14.95, "pml"=>0.01993}}, {"_id"=>5117.0, "value"=>{"s"=>3.7, "p"=>12.9, "pml"=>0.0172}}, {"_id"=>5118.0, "value"=>{"s"=>4.2, "p"=>12.9, "pml"=>0.0172}}, {"_id"=>5975.0}]
(rdb:1) collection.first.class
Moped::BSON::Document
(rdb:1) collection.to_a
.rvm/rubies/ruby-1.9.3-p194/lib/ruby/1.9.1/debug.rb:130:in `eval':undefined method `call' for #<Hash:0x007fa7bcf12ca8>
Trying this produces the same error:
collection = collection.each {|c| c.to_hash}.to_a
Any help appreciated. Thanks.