aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2019-04-06 11:37:34 +0900
committerRyuta Kamizono <kamipo@gmail.com>2019-04-06 11:37:34 +0900
commit56b4fdb52bd327d42aec9ee3dcea01298aafe945 (patch)
treec2dc09e95f247dc9180a5d371bd7b5c97918a541 /activerecord
parente9d1d7668604c176dc795e4f8759a2addfd9669c (diff)
downloadrails-56b4fdb52bd327d42aec9ee3dcea01298aafe945.tar.gz
rails-56b4fdb52bd327d42aec9ee3dcea01298aafe945.tar.bz2
rails-56b4fdb52bd327d42aec9ee3dcea01298aafe945.zip
Association loading isn't to be affected by null relation scoping
Follow up of #35868. Closes #19349.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation.rb8
-rw-r--r--activerecord/lib/active_record/scoping/named.rb2
-rw-r--r--activerecord/test/cases/scoping/relation_scoping_test.rb24
3 files changed, 31 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb
index c1ef98c063..c1cdad9c87 100644
--- a/activerecord/lib/active_record/relation.rb
+++ b/activerecord/lib/active_record/relation.rb
@@ -689,7 +689,7 @@ module ActiveRecord
end
def empty_scope? # :nodoc:
- @values == klass.unscoped.values
+ !null_relation? && @values == klass.unscoped.values
end
def has_limit_or_offset? # :nodoc:
@@ -721,6 +721,10 @@ module ActiveRecord
@loaded = true
end
+ def null_relation? # :nodoc:
+ is_a?(NullRelation)
+ end
+
private
def already_in_scope?
@delegate_to_klass && begin
@@ -770,7 +774,7 @@ module ActiveRecord
@records =
if eager_loading?
apply_join_dependency do |relation, join_dependency|
- if ActiveRecord::NullRelation === relation
+ if relation.null_relation?
[]
else
relation = join_dependency.apply_column_aliases(relation)
diff --git a/activerecord/lib/active_record/scoping/named.rb b/activerecord/lib/active_record/scoping/named.rb
index 681a5c6250..cd9801b7a0 100644
--- a/activerecord/lib/active_record/scoping/named.rb
+++ b/activerecord/lib/active_record/scoping/named.rb
@@ -58,7 +58,7 @@ module ActiveRecord
end
def default_extensions # :nodoc:
- if scope = current_scope || build_default_scope
+ if scope = scope_for_association || build_default_scope
scope.extensions
else
[]
diff --git a/activerecord/test/cases/scoping/relation_scoping_test.rb b/activerecord/test/cases/scoping/relation_scoping_test.rb
index 8ab0782bbf..50b514d464 100644
--- a/activerecord/test/cases/scoping/relation_scoping_test.rb
+++ b/activerecord/test/cases/scoping/relation_scoping_test.rb
@@ -421,6 +421,18 @@ class HasManyScopingTest < ActiveRecord::TestCase
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
magician = BadReference.find(1)
assert_equal [magician], people(:michael).bad_references
@@ -461,4 +473,16 @@ class HasAndBelongsToManyScopingTest < ActiveRecord::TestCase
assert_equal "a category...", @welcome.categories.what_are_you
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