提交 56b4fdb5 编写于 作者: R Ryuta Kamizono

Association loading isn't to be affected by null relation scoping

Follow up of #35868.

Closes #19349.
上级 e9d1d766
...@@ -689,7 +689,7 @@ def inspect ...@@ -689,7 +689,7 @@ def inspect
end end
def empty_scope? # :nodoc: def empty_scope? # :nodoc:
@values == klass.unscoped.values !null_relation? && @values == klass.unscoped.values
end end
def has_limit_or_offset? # :nodoc: def has_limit_or_offset? # :nodoc:
...@@ -721,6 +721,10 @@ def load_records(records) ...@@ -721,6 +721,10 @@ def load_records(records)
@loaded = true @loaded = true
end end
def null_relation? # :nodoc:
is_a?(NullRelation)
end
private private
def already_in_scope? def already_in_scope?
@delegate_to_klass && begin @delegate_to_klass && begin
...@@ -770,7 +774,7 @@ def exec_queries(&block) ...@@ -770,7 +774,7 @@ def exec_queries(&block)
@records = @records =
if eager_loading? if eager_loading?
apply_join_dependency do |relation, join_dependency| apply_join_dependency do |relation, join_dependency|
if ActiveRecord::NullRelation === relation if relation.null_relation?
[] []
else else
relation = join_dependency.apply_column_aliases(relation) relation = join_dependency.apply_column_aliases(relation)
......
...@@ -58,7 +58,7 @@ def default_scoped(scope = relation) # :nodoc: ...@@ -58,7 +58,7 @@ def default_scoped(scope = relation) # :nodoc:
end end
def default_extensions # :nodoc: def default_extensions # :nodoc:
if scope = current_scope || build_default_scope if scope = scope_for_association || build_default_scope
scope.extensions scope.extensions
else else
[] []
......
...@@ -421,6 +421,18 @@ def test_nested_scope_finder ...@@ -421,6 +421,18 @@ def test_nested_scope_finder
end end
end end
def test_none_scoping
Comment.none.scoping do
assert_equal 2, @welcome.comments.count
assert_equal "a comment...", @welcome.comments.what_are_you
end
Comment.where("1=1").scoping do
assert_equal 2, @welcome.comments.count
assert_equal "a comment...", @welcome.comments.what_are_you
end
end
def test_should_maintain_default_scope_on_associations def test_should_maintain_default_scope_on_associations
magician = BadReference.find(1) magician = BadReference.find(1)
assert_equal [magician], people(:michael).bad_references assert_equal [magician], people(:michael).bad_references
...@@ -461,4 +473,16 @@ def test_nested_scope_finder ...@@ -461,4 +473,16 @@ def test_nested_scope_finder
assert_equal "a category...", @welcome.categories.what_are_you assert_equal "a category...", @welcome.categories.what_are_you
end end
end end
def test_none_scoping
Category.none.scoping do
assert_equal 2, @welcome.categories.count
assert_equal "a category...", @welcome.categories.what_are_you
end
Category.where("1=1").scoping do
assert_equal 2, @welcome.categories.count
assert_equal "a category...", @welcome.categories.what_are_you
end
end
end end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册