Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix for deep includes on the same association.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4758 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit d7f780e0664ebb939ce76d987cd30c6eec496dc7 1 parent 8c4b599
Jeremy Kemper jeremy authored
2  activerecord/CHANGELOG
View
@@ -1,5 +1,7 @@
*SVN*
+* Fix for deep includes on the same association. [richcollins@gmail.com]
+
* Tweak fixtures so they don't try to use a non-ActiveRecord class. [Kevin Clark]
* Remove ActiveRecord::Base.reset since Dispatcher doesn't use it anymore. [Rick Olson]
11 activerecord/lib/active_record/associations.rb
View
@@ -1393,11 +1393,13 @@ def initialize(reflection, join_dependency, parent = nil)
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}"
+ @aliased_table_name = cascade_alias
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
end
- join_dependency.table_aliases[aliased_table_name] += 1
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
@@ -1529,6 +1531,11 @@ def table_name_and_alias
def interpolate_sql(sql)
instance_eval("%@#{sql.gsub('@', '\@')}@")
end
+
+ private
+ def cascade_alias
+ active_record.connection.table_alias_for "#{pluralize(reflection.name)}_#{parent_table_name}"
+ end
end
end
end
5 activerecord/test/associations_cascaded_eager_loading_test.rb
View
@@ -103,4 +103,9 @@ def test_eager_association_loading_of_stis_with_multiple_references
authors.first.posts.first.special_comments.first.post.very_special_comment
end
end
+
+ def test_eager_association_loading_with_recursive_cascaded_three_levels
+ root_node = RecursivelyCascadedTreeMixin.find(:first, :include=>{:children=>{:children=>:children}}, :order => 'mixins.id')
+ assert_equal mixins(:recursively_cascaded_tree_4), assert_no_queries { root_node.children.first.children.first.children.first }
+ end
end
4 activerecord/test/fixtures/mixin.rb
View
@@ -10,6 +10,10 @@ class TreeMixinWithoutOrder < Mixin
acts_as_tree :foreign_key => "parent_id"
end
+class RecursivelyCascadedTreeMixin < Mixin
+ acts_as_tree :foreign_key => "parent_id"
+end
+
class ListMixin < Mixin
acts_as_list :column => "pos", :scope => :parent
20 activerecord/test/fixtures/mixins.yml
View
@@ -39,6 +39,26 @@ tree_without_order_2:
type: TreeMixinWithoutOrder
parent_id:
+recursively_cascaded_tree_1:
+ id: 5005
+ type: RecursivelyCascadedTreeMixin
+ parent_id:
+
+recursively_cascaded_tree_2:
+ id: 5006
+ type: RecursivelyCascadedTreeMixin
+ parent_id: 5005
+
+recursively_cascaded_tree_3:
+ id: 5007
+ type: RecursivelyCascadedTreeMixin
+ parent_id: 5006
+
+recursively_cascaded_tree_4:
+ id: 5008
+ type: RecursivelyCascadedTreeMixin
+ parent_id: 5007
+
# List mixins
<% (1..4).each do |counter| %>
Please sign in to comment.
Something went wrong with that request. Please try again.