Description

ar_result_calculations adds methods to Array for when Array is an ActiveRecord result set. For example:

Product.all.sum(:price)
Product.all.average(:price)
Product.all.regression(:sales_volumn)

Will return the sum of the attribute :price from all rows. Note this is not the same as Product.sum(:price) which will create an SQL statement that does the summing.

Methods created on Array

All methods take one parameter (column name).

Method Description
sum Sum the given column. Delegates to super() if not an AR result set
mean Mean of the given column. Delegates to super() if not an AR result set. Aliases of avg and average
count Count the given column. Delegates to super() if not an AR result set
min Min the given column. Delegates to super() if not an AR result set
max Max the given column. Delegates to super() if not an AR result set
regression Ordinary Least Squares regression on the given column. Returns the regression as an array
slope Returns the slope of a regression on a given column
make_numeric_ Coerces a column to be numeric. Useful if you have derived columns returned from a query that are not in the model definition and hence are otherwise returned as strings. Returns self so is composable

License

(The MIT License)

Copyright © 2010 Kip Cole

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.