diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-14 07:19:47 +0000 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2006-08-14 07:19:47 +0000 |
commit | d7f780e0664ebb939ce76d987cd30c6eec496dc7 (patch) | |
tree | b042533b274e034805ff94fd0e6f9d50713f3592 | |
parent | 8c4b599b7c62b85069fa04533e8f06e5476f927a (diff) | |
download | rails-d7f780e0664ebb939ce76d987cd30c6eec496dc7.tar.gz rails-d7f780e0664ebb939ce76d987cd30c6eec496dc7.tar.bz2 rails-d7f780e0664ebb939ce76d987cd30c6eec496dc7.zip |
Fix for deep includes on the same association.
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4758 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
-rw-r--r-- | activerecord/CHANGELOG | 2 | ||||
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 11 | ||||
-rw-r--r-- | activerecord/test/associations_cascaded_eager_loading_test.rb | 5 | ||||
-rw-r--r-- | activerecord/test/fixtures/mixin.rb | 4 | ||||
-rw-r--r-- | activerecord/test/fixtures/mixins.yml | 20 |
5 files changed, 40 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index 5958ba6880..4c32d8c207 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -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] diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index c803560413..4f2d8a7999 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -1393,11 +1393,13 @@ module ActiveRecord 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 @@ module ActiveRecord 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 diff --git a/activerecord/test/associations_cascaded_eager_loading_test.rb b/activerecord/test/associations_cascaded_eager_loading_test.rb index 642e4fa433..20df8cd30e 100644 --- a/activerecord/test/associations_cascaded_eager_loading_test.rb +++ b/activerecord/test/associations_cascaded_eager_loading_test.rb @@ -103,4 +103,9 @@ class CascadedEagerLoadingTest < Test::Unit::TestCase 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 diff --git a/activerecord/test/fixtures/mixin.rb b/activerecord/test/fixtures/mixin.rb index 78cdbef9b3..afc976f8f1 100644 --- a/activerecord/test/fixtures/mixin.rb +++ b/activerecord/test/fixtures/mixin.rb @@ -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 diff --git a/activerecord/test/fixtures/mixins.yml b/activerecord/test/fixtures/mixins.yml index cb21349cd0..d7a0b97295 100644 --- a/activerecord/test/fixtures/mixins.yml +++ b/activerecord/test/fixtures/mixins.yml @@ -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| %> |