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

`scoping` should respect current class and STI constraint (#29199)

A relation includes `klass`, so it can not be used as it is if current
class is different from `current_scope.klass`. It should be created new
relation by current class to respect the klass and STI constraint.

Fixes #17603.
Fixes #23576.
上级 e0bef226
......@@ -24,8 +24,14 @@ module ClassMethods
# You can define a scope that applies to all finders using
# {default_scope}[rdoc-ref:Scoping::Default::ClassMethods#default_scope].
def all
current_scope = self.current_scope
if current_scope
current_scope.clone
if self == current_scope.klass
current_scope.clone
else
relation.merge!(current_scope)
end
else
default_scoped
end
......
......@@ -240,6 +240,20 @@ def test_scoping_is_correctly_restored
assert_nil SpecialComment.current_scope
end
def test_scoping_respects_current_class
Comment.unscoped do
assert_equal "a comment...", Comment.all.what_are_you
assert_equal "a special comment...", SpecialComment.all.what_are_you
end
end
def test_scoping_respects_sti_constraint
Comment.unscoped do
assert_equal comments(:greetings), Comment.find(1)
assert_raises(ActiveRecord::RecordNotFound) { SpecialComment.find(1) }
end
end
def test_circular_joins_with_scoping_does_not_crash
posts = Post.joins(comments: :post).scoping do
Post.first(10)
......
......@@ -60,6 +60,10 @@ def to_s
class SpecialComment < Comment
default_scope { where(deleted_at: nil) }
def self.what_are_you
"a special comment..."
end
end
class SubSpecialComment < SpecialComment
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册