The corresponding attributes on the object are updated, and the record is
saved. This method does cause validations to be run. Therefore, if the object is invalid, the
save will fail, and false will be returned.
ActiveRecord::Calculations::ClassMethods
Public Instance Methods
average(column_name, options = {})
Calculate the average of the data in the specified column with this attribute. Valid options are
the same as those for the calculate method.
Account.average('age')
calculate(operation, column_name, options = {})
This method calculates aggregate values of columns on the database table backing the Active
Record class. Valid operations are count(:count), sum(:sum), average(:avg), minimum(:min),
and maximum(:max). When the aggregate value is a single value, the type of the returned value is
Fixnum for count, Float for average, and the actual of the column we are aggregating for all
other operations.
Account.calculate(:count, :all) # The same as Account.count
Account.average(:age) # SELECT AVG(age) FROM accounts.
Pages:
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516