aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2018-11-30 21:41:12 +0900
committerGitHub <noreply@github.com>2018-11-30 21:41:12 +0900
commit5c6316dbb88075e27169f49a22e59357efd1a967 (patch)
treefb78cb1a719c36c7ee40c28882f41397f152c425 /activerecord
parentfaf91b0db432d6b9dc820c060ccce0b3d10d7daa (diff)
parent3090b358482942b944e5944c4869d2bf1afdefb8 (diff)
downloadrails-5c6316dbb88075e27169f49a22e59357efd1a967.tar.gz
rails-5c6316dbb88075e27169f49a22e59357efd1a967.tar.bz2
rails-5c6316dbb88075e27169f49a22e59357efd1a967.zip
Merge pull request #34572 from kamipo/fix_scoping_with_query_method
Fix the scoping with query methods in the scope block
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/relation/spawn_methods.rb2
-rw-r--r--activerecord/test/cases/scoping/relation_scoping_test.rb7
-rw-r--r--activerecord/test/models/post.rb1
3 files changed, 8 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/relation/spawn_methods.rb b/activerecord/lib/active_record/relation/spawn_methods.rb
index 562e04194c..7874c4c35a 100644
--- a/activerecord/lib/active_record/relation/spawn_methods.rb
+++ b/activerecord/lib/active_record/relation/spawn_methods.rb
@@ -8,7 +8,7 @@ module ActiveRecord
module SpawnMethods
# This is overridden by Associations::CollectionProxy
def spawn #:nodoc:
- clone
+ @delegate_to_klass ? klass.all : clone
end
# Merges in the conditions from <tt>other</tt>, if <tt>other</tt> is an ActiveRecord::Relation.
diff --git a/activerecord/test/cases/scoping/relation_scoping_test.rb b/activerecord/test/cases/scoping/relation_scoping_test.rb
index b4f4379e5e..b1f2ffe29c 100644
--- a/activerecord/test/cases/scoping/relation_scoping_test.rb
+++ b/activerecord/test/cases/scoping/relation_scoping_test.rb
@@ -254,11 +254,16 @@ class RelationScopingTest < ActiveRecord::TestCase
end
end
- def test_scoping_works_in_the_scope_block
+ def test_scoping_with_klass_method_works_in_the_scope_block
expected = SpecialPostWithDefaultScope.unscoped.to_a
assert_equal expected, SpecialPostWithDefaultScope.unscoped_all
end
+ def test_scoping_with_query_method_works_in_the_scope_block
+ expected = SpecialPostWithDefaultScope.unscoped.where(author_id: 0).to_a
+ assert_equal expected, SpecialPostWithDefaultScope.authorless
+ end
+
def test_circular_joins_with_scoping_does_not_crash
posts = Post.joins(comments: :post).scoping do
Post.first(10)
diff --git a/activerecord/test/models/post.rb b/activerecord/test/models/post.rb
index 710a75ad30..e32cc59399 100644
--- a/activerecord/test/models/post.rb
+++ b/activerecord/test/models/post.rb
@@ -254,6 +254,7 @@ class SpecialPostWithDefaultScope < ActiveRecord::Base
self.table_name = "posts"
default_scope { where(id: [1, 5, 6]) }
scope :unscoped_all, -> { unscoped { all } }
+ scope :authorless, -> { unscoped { where(author_id: 0) } }
end
class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base