DEV Community

Sohair Ahmad
Sohair Ahmad

Posted on

2

Rails - Run Default Scope on All queries

In a recent release candidate of Rails 6.1 (RC2) I have found a small but useful change in the active record library.

Consider a multi-tenant rails application, where everything was under the umbrella of the Organization.

default_scope used to run on all select and insert queries.

Previously:

class User < ApplicationRecord
  default_scope -> { where(organization_id: Current.organization.id) }
end
Enter fullscreen mode Exit fullscreen mode

Now, Rails has the ability to run this default_scope on select, insert, delete and update queries by just adding all_queries: true

class User < ApplicationRecord
  default_scope -> { where(organization_id: Current.organization.id) }, all_queries: true
end
Enter fullscreen mode Exit fullscreen mode

Top comments (0)

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay