mongoid/mongoid — Issue #1402: max (or any grouped operator) does not work with Time fields due to nan?
Author: KronicDeth
Created: 2011-11-02 14:06:50+00:00
State: open

class ResultsOnDate
field :start_at, :type => Time
end

ResultsOnDate.max(:start_at)

NoMethodError: undefined method `nan?' for 2011-03-17 05:00:00 UTC:Time

def grouped(start, field, reduce)
collection = klass.collection.group(
:cond => selector,
:initial => { start => "start" },
:reduce => reduce.gsub("[field]", field)
)
value = collection.empty? ? nil : collection.first[start.to_s]
value ? (value.nan? ? nil : value) : value
end

nan? only works for Numeric classes I think.