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

Ticket #5500: enumerable_calculations_sum_fix.diff

File enumerable_calculations_sum_fix.diff, 1.1 kB (added by coda.hale@gmail.com, 2 years ago)

tested fix, including unit tests to prevent the opposite from occuring

  • test/calculations_test.rb

    old new  
    147147    assert_equal 3, c['CLIENT'] 
    148148    assert_equal 2, c['FIRM'] 
    149149  end 
     150   
     151  def test_should_not_overshadow_enumerable_sum 
     152    assert_equal 6, [1, 2, 3].sum(&:abs) 
     153  end 
    150154 
    151155  def test_should_sum_scoped_field 
    152156    assert_equal 15, companies(:rails_core).companies.sum(:id) 
  • lib/active_record/associations/association_collection.rb

    old new  
    4141        delete(@target) 
    4242        reset_target! 
    4343      end 
     44       
     45      # Calculate sum using SQL, not Enumerable 
     46      def sum(*args) 
     47        calculate(:sum, *args) 
     48      end 
    4449 
    4550      # Remove +records+ from this association.  Does not destroy +records+. 
    4651      def delete(*records)