You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I followed the example of the Has and belongs to many. What I was expecting to happen is that when the following code is run, it would scope by the association.
The idea being the user could belong to many accounts but would only be found if the current_tenant was an account it was a member of.
When I run the above code the query it looks for an account_id on the User model. Producing an error when it can't find the foreign key.
PG::UndefinedColumn: ERROR: column users.account_id does not exist (ActiveRecord::StatementInvalid)
LINE 1: SELECT "users".* FROM "users" WHERE "users"."account_id" = $...
This is what I expected as the normal behaviour for acts_as_tenant :account. I thought adding the through: option would then allow for the user to belong to many accounts, but only return the one that is the current tenant.
What exactly does the through: option allows for?
Is there a common approach or best practice for a user belonging to many tenants?
Here is the code I used for the models
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_many :account_users
acts_as_tenant :account, through: :account_users
end
class AccountUser < ApplicationRecord
belongs_to :user
acts_as_tenant :account
end
I followed the example of the Has and belongs to many. What I was expecting to happen is that when the following code is run, it would scope by the association.
The idea being the user could belong to many accounts but would only be found if the
current_tenant
was an account it was a member of.When I run the above code the query it looks for an
account_id
on theUser
model. Producing an error when it can't find the foreign key.This is what I expected as the normal behaviour for
acts_as_tenant :account
. I thought adding thethrough:
option would then allow for the user to belong to many accounts, but only return the one that is the current tenant.What exactly does the
through:
option allows for?Is there a common approach or best practice for a user belonging to many tenants?
Here is the code I used for the models
The text was updated successfully, but these errors were encountered: