peterhume/mongoid-tree — pull request #1 by yoda (2011-11-14 02:05:15+00:00)
Title: Refresh

Hey Peter,

Its the map reduce used in grouped from mongoid:

MONGODB development['$cmd'].find({"group"=>{"ns"=>"projects", "$reduce"=>, "cond"=>{:deleted_at=>nil, :parent_id=>BSON::ObjectId('4dc35e260caa83661a000058'), :_id=>{"$ne"=>BSON::ObjectId('4dc737d90caa832d6600000e')}}, "initial"=>{:max=>"start"}}}).limit(-1)

Because position is 0 is gets evaluated as false if this occurs for all items in the selection then max is never redefined so when it returns to ruby:

value = collection.empty? ? nil : collection.first[start.to_s]

value && value.do_or_do_not(:nan?) ? nil : value

the collection is not empty so it evaluates collection.first[:max] which is still "start".

This fixes the bug from mongoid.

Regards,
Sam