diff options
author | Rizwan Reza <rizwanreza@gmail.com> | 2010-05-17 02:40:15 +0430 |
---|---|---|
committer | Rizwan Reza <rizwanreza@gmail.com> | 2010-05-17 02:40:15 +0430 |
commit | d148a6f6ba5f8ee65905f12cd2601fcc377d4852 (patch) | |
tree | 4bcb5e7ad47cfb9a9bb14ffe7c9e003d4646d64c /activerecord/lib/active_record/relation/calculations.rb | |
parent | e1c773006969abea3c0619fbdc7e32c121b6085f (diff) | |
parent | 6b4e0cc526f55b5532cf99292c94f0a4db53b16f (diff) | |
download | rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.tar.gz rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.tar.bz2 rails-d148a6f6ba5f8ee65905f12cd2601fcc377d4852.zip |
Merge branch 'master' of git://github.com/rails/rails
Diffstat (limited to 'activerecord/lib/active_record/relation/calculations.rb')
-rw-r--r-- | activerecord/lib/active_record/relation/calculations.rb | 11 |
1 files changed, 5 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/relation/calculations.rb b/activerecord/lib/active_record/relation/calculations.rb index 858d298470..44baeb6c84 100644 --- a/activerecord/lib/active_record/relation/calculations.rb +++ b/activerecord/lib/active_record/relation/calculations.rb @@ -45,9 +45,8 @@ module ActiveRecord calculate(:count, column_name, options) end - # Calculates the average value on a given column. The value is returned as - # a float, or +nil+ if there's no row. See +calculate+ for examples with - # options. + # Calculates the average value on a given column. Returns +nil+ if there's + # no row. See +calculate+ for examples with options. # # Person.average('age') # => 35.8 def average(column_name, options = {}) @@ -241,9 +240,9 @@ module ActiveRecord def type_cast_calculated_value(value, column, operation = nil) if value.is_a?(String) || value.nil? case operation - when 'count' then value.to_i - when 'sum' then type_cast_using_column(value || '0', column) - when 'average' then value && (value.is_a?(Fixnum) ? value.to_f : value).to_d + when 'count' then value.to_i + when 'sum' then type_cast_using_column(value || '0', column) + when 'average' then value.try(:to_d) else type_cast_using_column(value, column) end else |