Skip to content

Commit

Permalink
Merge pull request #34572 from kamipo/fix_scoping_with_query_method
Browse files Browse the repository at this point in the history
Fix the scoping with query methods in the scope block
  • Loading branch information
kamipo committed Nov 30, 2018
2 parents faf91b0 + 3090b35 commit 5c6316d
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/spawn_methods.rb
Expand Up @@ -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.
Expand Down
7 changes: 6 additions & 1 deletion activerecord/test/cases/scoping/relation_scoping_test.rb
Expand Up @@ -254,11 +254,16 @@ def test_scoping_respects_sti_constraint
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)
Expand Down
1 change: 1 addition & 0 deletions activerecord/test/models/post.rb
Expand Up @@ -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
Expand Down

0 comments on commit 5c6316d

Please sign in to comment.