简体   繁体   中英

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 (eg: john.realestate.dev ), everything is OK ( current_tenant ie john is set).

I have another model called Property ; when the current logged in tenant ie 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

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.

The real fix here is to make sure you include acts_as_tenant(:account) on every single model you want scoped. This is going to make sure it checks for the current_tenant before saving data.

You don't necessarily need the has_many and belongs_to relation, although it sounds like it may make sense for your application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM