Skip to content

Commit b96db52

Browse files
committed
port [9018] to 2.0 stable
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/2-0-stable@9019 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
1 parent 0e57097 commit b96db52

File tree

3 files changed

+11
-0
lines changed

3 files changed

+11
-0
lines changed

activerecord/CHANGELOG

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
*SVN*
22

3+
* Ensure that ActiveRecord::Calculations disambiguates field names with the table name. #11027 [cavalle]
4+
35
* Ensure that modifying has_and_belongs_to_many actions clear the query cache. Closes #10840 [john.andrews]
46

57
* Fix issue where Table#references doesn't pass a :null option to a *_type attribute for polymorphic associations. Closes #10753 [railsjitsu]

activerecord/lib/active_record/calculations.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ def construct_calculation_sql(operation, column_name, options) #:nodoc:
155155
scope = scope(:find)
156156
merged_includes = merge_includes(scope ? scope[:include] : [], options[:include])
157157
aggregate_alias = column_alias_for(operation, column_name)
158+
column_name = "#{connection.quote_table_name(table_name)}.#{column_name}" unless column_name == "*" || column_name.to_s.include?('.')
158159

159160
if operation == 'count'
160161
if merged_includes.any?

activerecord/test/associations/join_model_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,14 @@ def test_has_many_through_sum_uses_calculations
536536
assert_nothing_raised { authors(:david).comments.sum(:post_id) }
537537
end
538538

539+
def test_calculations_on_has_many_through_should_disambiguate_fields
540+
assert_nothing_raised { authors(:david).categories.maximum(:id) }
541+
end
542+
543+
def test_calculations_on_has_many_through_should_not_disambiguate_fields_unless_necessary
544+
assert_nothing_raised { authors(:david).categories.maximum("categories.id") }
545+
end
546+
539547
def test_has_many_through_has_many_with_sti
540548
assert_equal [comments(:does_it_hurt)], authors(:david).special_post_comments
541549
end

0 commit comments

Comments
 (0)