In some cases, you need to grab several COUNTs, SUMs etc from the database, and it speeds things along if you can get them all in one query. This patch provides a simple way to achieve this through ActiveRecord::Base.calculate. For example:
Person.calculate(:how_many => [:count, '*'], :total_age => [:sum, :age])
#=> {:how_many => 12, :total_age => 387}
Person.calculate({:how_many => [:count, '*'], :total_age => [:sum, :age]},
:conditions => ['age < ?', 30])
#=> {:how_many => 7, :total_age => 94}
The results are fetched using one query only, rather than a separate query for each quantity. I've found this tremendously useful for generating reports from large data sets.
I've included documentation and a few tests, which run fine under MySQL. Testing with other DBs would be appreciated.