未验证 提交 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
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)
......
......@@ -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")
......
......@@ -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
......
......@@ -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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册