datamapper/dm-aggregates — Issue #15: Avg returns 0.0 for empty table (dm-1.2.0)
Author: Ragmaanir
Created: 2011-10-31 13:03:36+00:00
State: open

Hi,

when i do

MyModel.avg(:something)

i get 0.0 for an empty table (sqlite3, integer property). I think this is wrong and should return nil instead (SQL AVG returns null).
I guess this is caused by the to_f in:

def avg(property, value)
property.primitive == ::Integer ? value.to_f : property.load(value)
end

in the `dm-aggregates/adapters/dm-do-adapter.rb`.

A quick workaround i use is to call MyModel.aggregate(:all.count, :something.sum) and compute the average myself.