Method: ActiveRecord::Relation#scoping
- Defined in:
- activerecord/lib/active_record/relation.rb
#scoping(all_queries: nil, &block) ⇒ Object
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.
541 542 543 544 545 546 547 548 549 550 |
# File 'activerecord/lib/active_record/relation.rb', line 541 def scoping(all_queries: nil, &block) registry = model.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 |