Tweaking scoping rules as part of acts_as_tenant?

176 Views Asked by At

I have a rails model Thing that uses acts_as_tenant to tie it to a company. This works fine, and now users who are part of a company can only access the Things associated with their company.

class Thing < ApplicationRecord
  acts_as_tenant(:company)
  belongs_to :company
end

Now I've added an is_global boolean field to my Thing. Now I would like for Thing (via acts_as_tenant) to instead be scoped such that users have access to their company's Things and any global Things. Is there a way to configure acts_as_tenant to enable this kind of behavior?

My current workaround is to abandon the acts_as_tenant(:company) call in this model and instead create a new default scope:

default_scope { where('company_id = ? or is_global = true', ActsAsTenant.current_tenant.id) } 

This seems to do the job, though I am concerned that I may be unknowingly bypassing some of the good features of acts_as_tenant here.

1

There are 1 best solutions below

0
On

Having some records in a table that are accessible by any tenant, and some records that are tenanted in the same table is kind of against the philosophy of multitenancy.

As well having "either one or another field is blank" is not a super great pattern.

I would suggest creating a clone of the global record for each tenant.