[Rails-core] ActiveRecord Calculations

Rick Olson technoweenie at gmail.com
Sat Feb 25 21:44:34 GMT 2006


Hot off the trac patches is my patch for ActiveRecord Calculations
(heavily based on my plugin): http://dev.rubyonrails.org/ticket/3958. 
Not only does it replace the count method, but it adds methods for
sum, maximum, minimum, and average.  I think the coolest part,
however, is using grouped queries against belongs_to assocations:

class Order < AR::Base
  belongs_to :person
end

orders = Order.calculate :sum, :quantity, :group => 'person_id'
orders['5']
=> 55

person = Person.find(5)
orders = Order.sum :quantity, :group => :person
orders[person]
=> 55

Scoped queries are possible too:

person.orders.sum :quantity
=> 55

I'm mostly interested in these things:

Is backwards compatibility maintained with #count?  So far tests pass
and it seems to work for me.

Are my data type assumptions good?  I'm always returning fixnums for
COUNT and floats for AVG.  Everything else depends on the column's
data type.

Is the documentation good?  If you're still confused on some aspects,
let me know so I can clarify a bit better.

Thanks everyone...

--
Rick Olson
http://techno-weenie.net


More information about the Rails-core mailing list