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

Avoid extra scoping in delegating to klass methods in the scope block #33394

Merged
merged 1 commit into from
Jul 19, 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
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/relation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -309,10 +309,10 @@ def cache_key(timestamp_column = :updated_at)
# Please check unscoped if you want to remove all previous scopes (including
# the default_scope) during the execution of a block.
def scoping
previous, klass.current_scope = klass.current_scope(true), self
previous, klass.current_scope = klass.current_scope(true), self unless @delegate_to_klass
yield
ensure
klass.current_scope = previous
klass.current_scope = previous unless @delegate_to_klass
end

def _exec_scope(*args, &block) # :nodoc:
Expand Down
5 changes: 5 additions & 0 deletions activerecord/test/cases/scoping/relation_scoping_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,11 @@ def test_scoping_respects_sti_constraint
end
end

def test_scoping_works_in_the_scope_block
expected = SpecialPostWithDefaultScope.unscoped.to_a
assert_equal expected, SpecialPostWithDefaultScope.unscoped_all
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
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ class SpecialPostWithDefaultScope < ActiveRecord::Base
self.inheritance_column = :disabled
self.table_name = "posts"
default_scope { where(id: [1, 5, 6]) }
scope :unscoped_all, -> { unscoped { all } }
end

class PostThatLoadsCommentsInAnAfterSaveHook < ActiveRecord::Base
Expand Down