From d72add9c20778e253f90fbfe587eae0e205c40ad Mon Sep 17 00:00:00 2001 From: Ernie Miller Date: Sat, 12 Feb 2011 13:59:53 -0500 Subject: Fix table name collision due to incorrect alias count on certain joins. [#6423 state:committed] Signed-off-by: Santiago Pastorino --- .../class_methods/join_dependency/join_association.rb | 8 ++++---- .../test/cases/associations/inner_join_association_test.rb | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb index 3fea24ebf8..856d826d67 100644 --- a/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb +++ b/activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb @@ -82,12 +82,12 @@ module ActiveRecord connection = active_record.connection name = connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}" - table_index = aliases[name] + 1 - name = name[0, connection.table_alias_length-3] + "_#{table_index}" if table_index > 1 + aliases[name] += 1 + name = name[0, connection.table_alias_length-3] + "_#{aliases[name]}" if aliases[name] > 1 + else + aliases[name] += 1 end - aliases[name] += 1 - name end diff --git a/activerecord/test/cases/associations/inner_join_association_test.rb b/activerecord/test/cases/associations/inner_join_association_test.rb index da2a81e98a..8f32902c19 100644 --- a/activerecord/test/cases/associations/inner_join_association_test.rb +++ b/activerecord/test/cases/associations/inner_join_association_test.rb @@ -16,6 +16,13 @@ class InnerJoinAssociationTest < ActiveRecord::TestCase assert_equal authors(:david), result.first end + def test_construct_finder_sql_does_not_table_name_collide_on_duplicate_associations + assert_nothing_raised do + sql = Person.joins(:agents => {:agents => :agents}).joins(:agents => {:agents => {:primary_contact => :agents}}).to_sql + assert_match(/agents_people_4/i, sql) + end + end + def test_construct_finder_sql_ignores_empty_joins_hash sql = Author.joins({}).to_sql assert_no_match(/JOIN/i, sql) -- cgit v1.2.3