未验证 提交 abcbfe87 编写于 作者: R Ryuta Kamizono 提交者: GitHub

Merge pull request #39378 from kamipo/fix_has_many_through_with_source_scope

Fix through association to respect source scope for `includes`/`preload`
...@@ -132,7 +132,9 @@ def scope ...@@ -132,7 +132,9 @@ def scope
end end
def reflection_scope 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 end
def build_scope def build_scope
...@@ -142,7 +144,7 @@ def build_scope ...@@ -142,7 +144,7 @@ def build_scope
scope.where!(reflection.type => model.polymorphic_name) scope.where!(reflection.type => model.polymorphic_name)
end end
scope.merge!(reflection_scope) if reflection.scope scope.merge!(reflection_scope) unless reflection_scope.empty_scope?
if preload_scope && !preload_scope.empty_scope? if preload_scope && !preload_scope.empty_scope?
scope.merge!(preload_scope) scope.merge!(preload_scope)
......
...@@ -1055,6 +1055,12 @@ def test_can_update_through_association ...@@ -1055,6 +1055,12 @@ def test_can_update_through_association
end end
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 def test_has_many_through_polymorphic_with_rewhere
post = TaggedPost.create!(title: "Tagged", body: "Post") post = TaggedPost.create!(title: "Tagged", body: "Post")
tag = post.tags.create!(name: "Tag") tag = post.tags.create!(name: "Tag")
......
...@@ -517,7 +517,7 @@ def test_nested_has_many_through_with_conditions_on_through_associations ...@@ -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 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) 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) blue = tags(:blue)
assert_no_queries do assert_no_queries do
...@@ -538,7 +538,7 @@ def test_nested_has_many_through_with_conditions_on_source_associations ...@@ -538,7 +538,7 @@ def test_nested_has_many_through_with_conditions_on_source_associations
end end
def test_nested_has_many_through_with_conditions_on_source_associations_preload 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) blue = tags(:blue)
assert_no_queries do assert_no_queries do
......
...@@ -176,6 +176,8 @@ def extension_method; end ...@@ -176,6 +176,8 @@ def extension_method; end
has_many :topics, primary_key: "name", foreign_key: "author_name" has_many :topics, primary_key: "name", foreign_key: "author_name"
has_many :lazy_readers_skimmers_or_not, through: :posts
attr_accessor :post_log attr_accessor :post_log
after_initialize :set_post_log after_initialize :set_post_log
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册