Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the scoping with query methods in the scope block #34572

Merged
merged 1 commit into from Nov 30, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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