Skip to content

Reference data from multiple tenants in "activerecord-multitenant" #261

@osyo-manga

Description

@osyo-manga

hi! I use this gem all the time, thanks!
By the way, I have a suggestion, could you please incorporate multi-tenant reference support into this gem?
The reason is that there is a case in which we need to acquire data across tenants, but we are having trouble achieving this with “activerecord-multitenant” at present.
To give a concrete example, we have a case where a group company is using an application and wants to be able to check data across multiple tenants in the group company.

The sample code is as follows.

tenant1, tenant2, tenant3 = Tenant.order(:created_at).take(3)
pp tenant1.id   # => "1310041a-c8a8-4d35-b395-8386af95a24f"
pp tenant2.id   # => "bb257a59-9811-473b-87a7-64c11f8799ab"
pp tenant3.id   # => "eff47159-53af-4b35-ba39-13e6fac0d5fa"

# Multiple `tenant1.id, tenant2.id, tenant3.id` queries are added
MultiTenant.with_read_multiple([tenant1.id, tenant2.id, tenant3.id]) do
  puts User.all.to_sql
  # => SELECT "users".*
  #      FROM "users"
  #     WHERE "users"."tenant_id" IN ('1310041a-c8a8-4d35-b395-8386af95a24f', 'bb257a59-9811-473b-87a7-64c11f8799ab', 'eff47159-53af-4b35-ba39-13e6fac0d5fa')
end

At this point, reading alone is sufficient, so we are not considering support up to writing

I'll make a PR if there is interest or acceptance of this proposal!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions