Xodarap
Xodarap

Reputation: 11849

Apartment active record query across tenants

I use Apartment for multi-tenancy. Is there any way that I can run a query across all tenants instead of just my current one?

One kind of annoying way to do it would be something like

tenants.map do | tenant |
  Apartment::Tenant.switch! tenant
  User.all
end

I'm not sure what the side effects of switching tenants are though, and it would be nice if there was some way to set the tenant at a query level.

Upvotes: 3

Views: 2359

Answers (2)

Pedro Schmitt
Pedro Schmitt

Reputation: 150

Another way would be:

Apartment::Tenant.each do
  User.all
end

Upvotes: 1

Xodarap
Xodarap

Reputation: 11849

One slightly better way of doing things is

tenants.map do | tenant |
  Apartment::Tenant.switch(tenant) do
    User.all
  end
end

This way it's not changing the current tenant

Upvotes: 3

Related Questions