Reputation: 5953
I have added the gem 'acts_as_tenant' to my Rails app.
I have set the following in the application controller:
set_current_tenant_by_subdomain(:tenant, :subdomain)
I have also added columns of information to the Tenants table. For instance the statuscode_id for a 'closed' status. In the Request model, I would like to scope using that column.
Something like this:
scope :closed, where(:statuscode_id => Tenant.find(current_tenant.id).request_closed)
But, current_tenant
isn't working.
What can I use to get the id of the current tenant?
Thanks!
Upvotes: 0
Views: 2903
Reputation:
All you've done so far is say that the subdomain will be used to define the tenant. In order to restrict the tenant to only see their data you need to define this in the model.
You need to add acts_as_tenant(:tenant)
to your Request
model. You will also need a tenant_id
column in the database for this table.
Once you've done this, the gem will take care of the rest and all your database access will automatically take care of restricting access by tenant. e.g. Request.all
will use default scope behind the scenes to restrict to only the requests owned by that tenant.
Please see ErwinM/acts_as_tenant for more information on this.
Upvotes: 1