1

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.

Chris Farmer
  • 24,974
  • 34
  • 121
  • 164
  • I don't think there's a way within ActsAsTenant to set this up, but you're achieving the same thing doing it this way. The only real config option that I've seen is the ability for ActsAsTenant to enforce always having the tenant set. But that's not affecting you here. As an aside, I don't think you need the `belongs_to` relationship when you have `acts_as_tenant` designated. – Travis Smith Jun 02 '20 at 09:53

1 Answers1

0

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.

Yshmarov
  • 3,450
  • 1
  • 22
  • 41