ErwinM / acts_as_tenant

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

Support has_many :through relationships #307

Closed HashNotAdam closed 5 months ago

HashNotAdam commented 1 year ago

Fixes #283

While previous commits have stated that they support HABTM relationships, acts_as_tenant attempts to set the foreign_key on the record rather than in an association.

This was proven by the new spec in spec/models/model_extensions_spec.rb which fails with the error:

can't write unknown attribute account_id

This PR makes acts_as_tenant aware that these relationships require a new record to be created on the association (unless one already exists).

BREAKING CHANGE

Given an assumption is being made that singular tenant/association names are belongs_to and plural names are has_many, acts_as_tenant will no longer attempt to create a new association if one already exists. This will allow those with non-standard associations to manually define the relationship

trevorrjohn commented 5 months ago

I was thinking about this PR. And for my use case it doesn't really make sense. A user can have multiple tenants, however when I am allowing the user to switch tenants, there is no current tenant selected. So defining the relationship as a normal has_many makes more sense to me.

Maybe I am missing something though.

HashNotAdam commented 5 months ago

I'm afraid I've dropped Acts As Tenant because I was having too many problems, so I'll just close this