ErwinM / acts_as_tenant

Easy multi-tenancy for Rails in a shared database setup.
MIT License
1.53k stars 262 forks source link

Acts as tenant through a belongs to association #293

Closed jimmypoulsen closed 1 year ago

jimmypoulsen commented 1 year ago

Hi.

We're currently implementing multi-tenancy in our single tenant application. Most of our application involves our CaseFile model. A case file has many offers, time entries and expenses. See the following model:

account

Neither offers, time entries or expenses can exist without a case file so I want to avoid having the account association on each of these models. I only want to have it on CaseFile.

Is this possible?

excid3 commented 1 year ago

I don't think we plan to support that. It's safer to have the tenant_id on each model because they can then be queried directly without the joins and just generally protects you since every record knows which tenant they belong to.

excid3 commented 1 year ago

Actually, this may do what you wanted: https://github.com/ErwinM/acts_as_tenant/commit/a05d9a2e07dc396fd56c857e7934bef67bd9dbc9

jimmypoulsen commented 1 year ago

I came to the same conclusion because of the issue with using joins so I just implemented the tenant_id on all tenant records. I don't think a05d9a2 would have solved my problem as I wasn't looking for a way to associate them using a has_and_belongs_to_many association. In my example a case file has many offers but an offer only belongs to one case file.