instance method
scoping
Ruby on Rails 7.0.10
Since v3.0.20Signature
scoping(all_queries: nil, &block)
Scope all queries to the current scope.
Comment.where(post_id: 1).scoping do
Comment.first
end
# => SELECT "comments".* FROM "comments" WHERE "comments"."post_id" = 1 ORDER BY "comments"."id" ASC LIMIT 1
If all_queries: true is passed, scoping will apply to all queries for the relation including update and delete on instances. Once all_queries is set to true it cannot be set to false in a nested block.
Please check unscoped if you want to remove all previous scopes (including the default_scope) during the execution of a block.
Parameters
-
all_querieskey = nil -
blockblock
Source
# File activerecord/lib/active_record/relation.rb, line 421
def scoping(all_queries: nil, &block)
registry = klass.scope_registry
if global_scope?(registry) && all_queries == false
raise ArgumentError, "Scoping is set to apply to all queries and cannot be unset in a nested block."
elsif already_in_scope?(registry)
yield
else
_scoping(self, registry, all_queries, &block)
end
end
Defined in activerecord/lib/active_record/relation.rb line 421
· View on GitHub
· Improve this page
· Find usages on GitHub
Defined in ActiveRecord::Relation