aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRick Olson <technoweenie@gmail.com>2008-03-13 03:06:51 +0000
committerRick Olson <technoweenie@gmail.com>2008-03-13 03:06:51 +0000
commit7750afc4c1b6873bf1ee50ca386b9714430be618 (patch)
treeb26252b2cd340fa7848a73990f5c1958cc395568 /activerecord
parentdd665ff9679514e354336bb183a9b40ad17145fb (diff)
downloadrails-7750afc4c1b6873bf1ee50ca386b9714430be618.tar.gz
rails-7750afc4c1b6873bf1ee50ca386b9714430be618.tar.bz2
rails-7750afc4c1b6873bf1ee50ca386b9714430be618.zip
Ensure that ActiveRecord::Calculations disambiguates field names with the table name. Closes #11027 [cavalle]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9018 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/calculations.rb1
-rw-r--r--activerecord/test/cases/associations/join_model_test.rb8
3 files changed, 11 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index f999877be8..8026f24023 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Ensure that ActiveRecord::Calculations disambiguates field names with the table name. #11027 [cavalle]
+
* Added add/remove_timestamps to the schema statements for adding the created_at/updated_at columns on existing tables #11129 [jramirez]
* Added ActiveRecord::Base.find(:last) #11338 [miloops]
diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb
index c8c8d42def..fc61a221a1 100644
--- a/activerecord/lib/active_record/calculations.rb
+++ b/activerecord/lib/active_record/calculations.rb
@@ -155,6 +155,7 @@ module ActiveRecord
scope = scope(:find)
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
aggregate_alias = column_alias_for(operation, column_name)
+ column_name = "#{connection.quote_table_name(table_name)}.#{column_name}" unless column_name == "*" || column_name.to_s.include?('.')
if operation == 'count'
if merged_includes.any?
diff --git a/activerecord/test/cases/associations/join_model_test.rb b/activerecord/test/cases/associations/join_model_test.rb
index c24a94bfc4..810e477ac0 100644
--- a/activerecord/test/cases/associations/join_model_test.rb
+++ b/activerecord/test/cases/associations/join_model_test.rb
@@ -544,6 +544,14 @@ class AssociationsJoinModelTest < ActiveRecord::TestCase
assert_nothing_raised { authors(:david).comments.sum(:post_id) }
end
+ def test_calculations_on_has_many_through_should_disambiguate_fields
+ assert_nothing_raised { authors(:david).categories.maximum(:id) }
+ end
+
+ def test_calculations_on_has_many_through_should_not_disambiguate_fields_unless_necessary
+ assert_nothing_raised { authors(:david).categories.maximum("categories.id") }
+ end
+
def test_has_many_through_has_many_with_sti
assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
end