diff --git a/activerecord/lib/active_record/associations/through_association.rb b/activerecord/lib/active_record/associations/through_association.rb index 53c5c3cedf99546a625143cc93fb7af11b55b2ed..81172179e04c22f606b9d8d26f22869c21c45855 100644 --- a/activerecord/lib/active_record/associations/through_association.rb +++ b/activerecord/lib/active_record/associations/through_association.rb @@ -16,7 +16,7 @@ def target_scope chain[1..-1].each do |reflection| scope = scope.merge( reflection.klass.scoped.with_default_scope. - except(:select, :create_with) + except(:select, :create_with, :includes) ) end scope diff --git a/activerecord/test/cases/associations/eager_test.rb b/activerecord/test/cases/associations/eager_test.rb index 3e92a77830a7a50e305c725e618d3072be4d3591..ea186be9ddfbe5a60dfb547c9b2ddd79c11d9275 100644 --- a/activerecord/test/cases/associations/eager_test.rb +++ b/activerecord/test/cases/associations/eager_test.rb @@ -448,6 +448,12 @@ def test_eager_with_has_many_through_with_conditions_join_model_with_include assert_equal post_tags, eager_post_tags end + def test_eager_with_has_many_through_join_model_ignores_default_includes + assert_nothing_raised do + authors(:david).comments_on_posts_with_default_include.to_a + end + end + def test_eager_with_has_many_and_limit posts = Post.find(:all, :order => 'posts.id asc', :include => [ :author, :comments ], :limit => 2) assert_equal 2, posts.size diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index e0cbc4426521c266c9e8bb5c4d5a440d9adf8689..23db5650d4719812be1953439734f4e136b1e110 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -138,6 +138,9 @@ def testing_proxy_target has_many :misc_post_first_blue_tags_2, :through => :posts, :source => :first_blue_tags_2, :conditions => { :posts => { :title => ['misc post by bob', 'misc post by mary'] } } + has_many :posts_with_default_include, :class_name => 'PostWithDefaultInclude' + has_many :comments_on_posts_with_default_include, :through => :posts_with_default_include, :source => :comments + scope :relation_include_posts, includes(:posts) scope :relation_include_tags, includes(:tags) diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb index 80296032bb1ca5930f0a1d133aacd89614165718..f2ab7b053e963b78e48f5802b3a9a61ac382bd65 100644 --- a/activerecord/test/models/post.rb +++ b/activerecord/test/models/post.rb @@ -162,3 +162,9 @@ class FirstPost < ActiveRecord::Base has_many :comments, :foreign_key => :post_id has_one :comment, :foreign_key => :post_id end + +class PostWithDefaultInclude < ActiveRecord::Base + self.table_name = 'posts' + default_scope includes(:comments) + has_many :comments, :foreign_key => :post_id +end \ No newline at end of file