diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index 149699daafad55d9e0291a05ba3a82933e4a5e3b..5fa9abad455588cb5bb39a8435628e0730b054db 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -132,7 +132,9 @@ def scope end def reflection_scope - @reflection_scope ||= reflection.scope ? reflection.scope_for(klass.unscoped) : klass.unscoped + @reflection_scope ||= begin + reflection.join_scopes(klass.arel_table, klass.predicate_builder).inject(klass.unscoped, &:merge!) + end end def build_scope @@ -142,7 +144,7 @@ def build_scope scope.where!(reflection.type => model.polymorphic_name) end - scope.merge!(reflection_scope) if reflection.scope + scope.merge!(reflection_scope) unless reflection_scope.empty_scope? if preload_scope && !preload_scope.empty_scope? scope.merge!(preload_scope) diff --git a/activerecord/test/cases/associations/has_many_through_associations_test.rb b/activerecord/test/cases/associations/has_many_through_associations_test.rb index 9d2691e59fa932048bce3fa865c17bcbe2ac4d21..ed9ae1df5f39ae30caa48bdd40dded810b6fae0e 100644 --- a/activerecord/test/cases/associations/has_many_through_associations_test.rb +++ b/activerecord/test/cases/associations/has_many_through_associations_test.rb @@ -1055,6 +1055,12 @@ def test_can_update_through_association end end + def test_has_many_through_with_source_scope + expected = [readers(:michael_welcome).becomes(LazyReader)] + assert_equal expected, Author.preload(:lazy_readers_skimmers_or_not).first.lazy_readers_skimmers_or_not + assert_equal expected, Author.eager_load(:lazy_readers_skimmers_or_not).first.lazy_readers_skimmers_or_not + end + def test_has_many_through_polymorphic_with_rewhere post = TaggedPost.create!(title: "Tagged", body: "Post") tag = post.tags.create!(name: "Tag") diff --git a/activerecord/test/cases/associations/nested_through_associations_test.rb b/activerecord/test/cases/associations/nested_through_associations_test.rb index 1205d5c741ef1d6c39ba77035a89ab46678081bb..1f649ca0a214ff935a3c0f2baa36805e3bee1683 100644 --- a/activerecord/test/cases/associations/nested_through_associations_test.rb +++ b/activerecord/test/cases/associations/nested_through_associations_test.rb @@ -517,7 +517,7 @@ def test_nested_has_many_through_with_conditions_on_through_associations def test_nested_has_many_through_with_conditions_on_through_associations_preload assert_empty Author.where("tags.id" => 100).joins(:misc_post_first_blue_tags) - author = assert_queries(3) { Author.includes(:misc_post_first_blue_tags).third } + author = assert_queries(2) { Author.includes(:misc_post_first_blue_tags).third } blue = tags(:blue) assert_no_queries do @@ -538,7 +538,7 @@ def test_nested_has_many_through_with_conditions_on_source_associations end def test_nested_has_many_through_with_conditions_on_source_associations_preload - author = assert_queries(4) { Author.includes(:misc_post_first_blue_tags_2).third } + author = assert_queries(2) { Author.includes(:misc_post_first_blue_tags_2).third } blue = tags(:blue) assert_no_queries do diff --git a/activerecord/test/models/author.rb b/activerecord/test/models/author.rb index 02c5edfc6dc79a6cfe8d8978c8029f034b2bac43..a44479e8580dcafe61f092b16cbe4581ca171469 100644 --- a/activerecord/test/models/author.rb +++ b/activerecord/test/models/author.rb @@ -176,6 +176,8 @@ def extension_method; end has_many :topics, primary_key: "name", foreign_key: "author_name" + has_many :lazy_readers_skimmers_or_not, through: :posts + attr_accessor :post_log after_initialize :set_post_log