Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 9243

Show
Ignore:
Timestamp:
04/08/08 05:20:33 (5 months ago)
Author:
rick
Message:

ActiveRecord::Base#sum defaults to 0 if no rows are returned. Closes #11550 [kamal]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r9235 r9243  
    11*SVN* 
     2 
     3* ActiveRecord::Base#sum defaults to 0 if no rows are returned.  Closes #11550 [kamal] 
    24 
    35* Ensure that respond_to? considers dynamic finder methods. Closes #11538. [floehopper] 
  • trunk/activerecord/lib/active_record/calculations.rb

    r9223 r9243  
    7272      #   Person.sum('age') 
    7373      def sum(column_name, options = {}) 
    74         calculate(:sum, column_name, options) 
     74        calculate(:sum, column_name, options) || 0 
    7575      end 
    7676 
  • trunk/activerecord/test/cases/calculations_test.rb

    r9223 r9243  
    9696  def test_should_sum_field_with_conditions 
    9797    assert_equal 105, Account.sum(:credit_limit, :conditions => 'firm_id = 6') 
     98  end 
     99 
     100  def test_should_return_zero_if_sum_conditions_return_nothing 
     101    assert_equal 0, Account.sum(:credit_limit, :conditions => '1 = 2') 
    98102  end 
    99103