diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-02 04:28:42 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2008-02-02 04:28:42 +0000 |
commit | 5bbc461fcbe18b0ab69c459a94cf0b58f5a4e6b9 (patch) | |
tree | 8fa372dcec9d6ae9822038cb590fbce4b92a6f4d /activerecord | |
parent | 6d8534acc032ab3a1ecbb88e9a0ce09d18734071 (diff) | |
download | rails-5bbc461fcbe18b0ab69c459a94cf0b58f5a4e6b9.tar.gz rails-5bbc461fcbe18b0ab69c459a94cf0b58f5a4e6b9.tar.bz2 rails-5bbc461fcbe18b0ab69c459a94cf0b58f5a4e6b9.zip |
Fix calculations on associations with custom :foreign_key. Closes #8117 [kamal, jack]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 2 | ||||
-rw-r--r-- | activerecord/test/cases/calculations_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/cases/invalid_date_test.rb | 24 |
3 files changed, 33 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index e14700f93b..c8c8d42def 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -213,7 +213,7 @@ module ActiveRecord group_attr = options[:group].to_s association = reflect_on_association(group_attr.to_sym) associated = association && association.macro == :belongs_to # only count belongs_to associations - group_field = (associated ? "#{options[:group]}_id" : options[:group]).to_s + group_field = associated ? association.primary_key_name : group_attr group_alias = column_alias_for(group_field) group_column = column_for group_field sql = construct_calculation_sql(operation, column_name, options.merge(:group_field => group_field, :group_alias => group_alias)) diff --git a/activerecord/test/cases/calculations_test.rb b/activerecord/test/cases/calculations_test.rb index 7e7beefb77..14987183eb 100644 --- a/activerecord/test/cases/calculations_test.rb +++ b/activerecord/test/cases/calculations_test.rb @@ -160,6 +160,14 @@ class CalculationsTest < ActiveRecord::TestCase end end + def test_should_calculate_grouped_association_with_foreign_key_option + Account.belongs_to :another_firm, :class_name => 'Firm', :foreign_key => 'firm_id' + c = Account.count(:all, :group => :another_firm) + assert_equal 1, c[companies(:first_firm)] + assert_equal 2, c[companies(:rails_core)] + assert_equal 1, c[companies(:first_client)] + end + def test_should_not_modify_options_when_using_includes options = {:conditions => 'companies.id > 1', :include => :firm} options_copy = options.dup diff --git a/activerecord/test/cases/invalid_date_test.rb b/activerecord/test/cases/invalid_date_test.rb new file mode 100644 index 0000000000..e2bb17c37f --- /dev/null +++ b/activerecord/test/cases/invalid_date_test.rb @@ -0,0 +1,24 @@ +require 'cases/helper' +require 'models/topic' + +class InvalidDateTest < Test::Unit::TestCase + def test_assign_valid_dates + valid_dates = [[2007, 11, 30], [1993, 2, 28], [2008, 2, 29]] + + invalid_dates = [[2007, 11, 31], [1993, 2, 29], [2007, 2, 29]] + + topic = Topic.new + + valid_dates.each do |date_src| + topic = Topic.new("last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s) + assert_equal(topic.last_read, Date.new(*date_src)) + end + + invalid_dates.each do |date_src| + assert_nothing_raised do + topic = Topic.new({"last_read(1i)" => date_src[0].to_s, "last_read(2i)" => date_src[1].to_s, "last_read(3i)" => date_src[2].to_s}) + assert_equal(topic.last_read, Time.local(*date_src).to_date, "The date should be modified according to the behaviour of the Time object") + end + end + end +end |