Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support has_many :through relationships #307

Closed

Conversation

HashNotAdam
Copy link
Contributor

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

While there is some support for "through" relationships, when finding
the association, the pluralized foreign key is used but join tables
use singular key names

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
@HashNotAdam HashNotAdam force-pushed the support_has_many_through branch from 1a1cd94 to 6d75aa4 Compare February 15, 2023 23:54
@HashNotAdam HashNotAdam force-pushed the support_has_many_through branch 2 times, most recently from 3c1ae11 to 5761008 Compare March 29, 2023 05:51
ActsAsTenant assumes that it needs to create an association with the
tenant unless a record already exists when calling the tenant
association. Unfortunately, until the parent record has been persisted,
Active Record will not be able to find the association using the tenant
association because the join record will not have an ID
@trevorrjohn
Copy link

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
Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Has and belongs to many though option not scoping as expected
2 participants