Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix duplicate table alias error when including an association with a …

…has_many :through association on the same join table. Closes #7310 [cavalle]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9095 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 1648208ab09277467220efc92a532994024eea78 1 parent 628ffad
@technoweenie technoweenie authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Fix duplicate table alias error when including an association with a has_many :through association on the same join table. Closes #7310 [cavalle]
+
* More efficient association preloading code that compacts a through_records array in a central location. Closes #11427 [danger]
* Improve documentation. [Radar, Jan De Poorter, chuyeow, xaviershay, danger, miloops, Xavier Noria, Sunny Ripert]
View
52 activerecord/lib/active_record/associations.rb
@@ -1727,36 +1727,19 @@ def initialize(reflection, join_dependency, parent = nil)
end
super(reflection.klass)
+ @join_dependency = join_dependency
@parent = parent
@reflection = reflection
@aliased_prefix = "t#{ join_dependency.joins.size }"
- @aliased_table_name = table_name #.tr('.', '_') # start with the table name, sub out any .'s
@parent_table_name = parent.active_record.table_name
-
- if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{aliased_table_name.downcase}\son}
- join_dependency.table_aliases[aliased_table_name] += 1
- end
-
- unless join_dependency.table_aliases[aliased_table_name].zero?
- # if the table name has been used, then use an alias
- @aliased_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}"
- table_index = join_dependency.table_aliases[aliased_table_name]
- join_dependency.table_aliases[aliased_table_name] += 1
- @aliased_table_name = @aliased_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
- else
- join_dependency.table_aliases[aliased_table_name] += 1
+ @aliased_table_name = aliased_table_name_for(table_name)
+
+ if reflection.macro == :has_and_belongs_to_many
+ @aliased_join_table_name = aliased_table_name_for(reflection.options[:join_table], "_join")
end
-
- if reflection.macro == :has_and_belongs_to_many || (reflection.macro == :has_many && reflection.options[:through])
- @aliased_join_table_name = reflection.macro == :has_and_belongs_to_many ? reflection.options[:join_table] : reflection.through_reflection.klass.table_name
- unless join_dependency.table_aliases[aliased_join_table_name].zero?
- @aliased_join_table_name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}_join"
- table_index = join_dependency.table_aliases[aliased_join_table_name]
- join_dependency.table_aliases[aliased_join_table_name] += 1
- @aliased_join_table_name = @aliased_join_table_name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
- else
- join_dependency.table_aliases[aliased_join_table_name] += 1
- end
+
+ if reflection.macro == :has_many && reflection.options[:through]
+ @aliased_join_table_name = aliased_table_name_for(reflection.through_reflection.klass.table_name, "_join")
end
end
@@ -1893,6 +1876,25 @@ def association_join
end
protected
+
+ def aliased_table_name_for(name, suffix = nil)
+ if !parent.table_joins.blank? && parent.table_joins.to_s.downcase =~ %r{join(\s+\w+)?\s+#{name.downcase}\son}
+ @join_dependency.table_aliases[name] += 1
+ end
+
+ unless @join_dependency.table_aliases[name].zero?
+ # if the table name has been used, then use an alias
+ name = active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}#{suffix}"
+ table_index = @join_dependency.table_aliases[name]
+ @join_dependency.table_aliases[name] += 1
+ name = name[0..active_record.connection.table_alias_length-3] + "_#{table_index+1}" if table_index > 0
+ else
+ @join_dependency.table_aliases[name] += 1
+ end
+
+ name
+ end
+
def pluralize(table_name)
ActiveRecord::Base.pluralize_table_names ? table_name.to_s.pluralize : table_name
end
View
7 activerecord/test/cases/associations_test.rb
@@ -551,7 +551,8 @@ def test_eager_has_one_through_polymorphic_with_source_type
class HasManyAssociationsTest < ActiveRecord::TestCase
fixtures :accounts, :companies, :developers, :projects,
- :developers_projects, :topics, :authors, :comments, :author_addresses
+ :developers_projects, :topics, :authors, :comments, :author_addresses,
+ :people, :posts
def setup
Client.destroyed_client_ids.clear
@@ -1318,6 +1319,10 @@ def test_dynamic_find_all_order_should_override_association_limit_for_through
assert_equal 4, authors(:david).limited_comments.find(:all, :conditions => "comments.type = 'SpecialComment'", :limit => 9_000).length
assert_equal 4, authors(:david).limited_comments.find_all_by_type('SpecialComment', :limit => 9_000).length
end
+
+ def test_find_all_include_over_the_same_table_for_through
+ assert_equal 2, people(:michael).posts.find(:all, :include => :people).length
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.