ruby-on-railsrubymulti-tenantacts-as-tenant

acts as tenant gem scopes all users data, ignoring scoping data based on the subdomain


I am using acts_as_tenant gem to do a multi-tenant app. I followed the instructions. I am using sub-domains.

In my application_controller.rb I have:

set_current_tenant_by_subdomain(:account, :subdomain)

I am using Account as the tenant. In my User model I called:

acts_as_tenant(:account)

The Problem

When I log into an account using a subdomain (e.g: john.realestate.dev), everything is OK (current_tenant i.e john is set).

I have another model called Property; when the current logged in tenant i.e john creates a new property, that record is seen by all the other users. I want only john to be able see the record he has created.

Where am I going wrong?

my models relationships are:-

Account - has_many :users

User - belongs_to :account

Property


Solution

  • i fixed my issue by creating a relationship between Account(has_many :properties) and Property(Belongs_to :account).

    then i added acts_as_tenant(:account) to the Property Model. I ran a migration that adds account_id to the property model.

    now it scopes data only created by the logged in user according to the subdomain.

    i fixed it by re-reading the gem's doc multiple times until i made sense of this statement from the doc.

    acts_as_tenant requires each scoped model to have a column in its schema linking it to a tenant. Adding acts_as_tenant to your model declaration will scope that model to the current tenant BUT ONLY if a current tenant has been set.

    Any ideas, suggestions on my implementation are mob welcome.