diff options
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 3 | ||||
-rw-r--r-- | activerecord/test/calculations_test.rb | 20 |
3 files changed, 24 insertions, 1 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 5eb64e75f1..3cde1dce39 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Calculations support non-numeric foreign keys. #8154 [kamal] + * with_scope is protected. #8524 [Josh Peek] * Quickref for association methods. #7723 [marclove, Mindsweeper] diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index 09c0c979e6..ef59db915a 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -235,7 +235,8 @@ module ActiveRecord end calculated_data.inject(ActiveSupport::OrderedHash.new) do |all, row| - key = associated ? key_records[row[group_alias].to_i] : type_cast_calculated_value(row[group_alias], group_column) + key = type_cast_calculated_value(row[group_alias], group_column) + key = key_records[key] if associated value = row[aggregate_alias] all << [key, type_cast_calculated_value(value, column, operation)] end diff --git a/activerecord/test/calculations_test.rb b/activerecord/test/calculations_test.rb index 22ef78ce40..bf34beb702 100644 --- a/activerecord/test/calculations_test.rb +++ b/activerecord/test/calculations_test.rb @@ -139,6 +139,26 @@ class CalculationsTest < Test::Unit::TestCase assert_equal 2, c[companies(:rails_core)] assert_equal 1, c[companies(:first_client)] end + + uses_mocha 'group_by_non_numeric_foreign_key_association' do + def test_should_group_by_association_with_non_numeric_foreign_key + ActiveRecord::Base.connection.expects(:select_all).returns([{"count_all" => 1, "firm_id" => "ABC"}]) + + firm = mock() + firm.expects(:id).returns("ABC") + firm.expects(:class).returns(Firm) + Company.expects(:find).with(["ABC"]).returns([firm]) + + column = mock() + column.expects(:name).at_least_once.returns(:firm_id) + column.expects(:type_cast).with("ABC").returns("ABC") + Account.expects(:columns).at_least_once.returns([column]) + + c = Account.count(:all, :group => :firm) + assert_equal Firm, c.first.first.class + assert_equal 1, c.first.last + end + end def test_should_not_modify_options_when_using_includes options = {:conditions => 'companies.id > 1', :include => :firm} |