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

Has and belongs to many though option not scoping as expected #283

Open
aarondufall opened this issue May 18, 2022 · 3 comments
Open

Has and belongs to many though option not scoping as expected #283

aarondufall opened this issue May 18, 2022 · 3 comments

Comments

@aarondufall
Copy link

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.

account = Account.first
ActsAsTenant.current_tenant = account
User.where(email: "[email protected]").first

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
class Account < ApplicationRecord
  has_many :account_users
  has_many :users, through: :account_users

  validates :name, presence: true, uniqueness: true
  validates :subdomain, presence: true, uniqueness: true
end
@michaelnera
Copy link

Got the same error.

I think it's probably because of this line:

belongs_to tenant, **valid_options

belongs_to tenant, **valid_options

It doesn't have any has_many association, only belongs_to

@yorsant
Copy link

yorsant commented Sep 28, 2023

Any updates? Is #307 going to be approved any time soon?

@giangdatoro
Copy link

giangdatoro commented Feb 29, 2024

@aarondufall

actually you can do this way

# AccountUser.rb

def self.users
  user_ids = self.all.pluck(:user_id).uniq
  User.unscoped.where(id: user_ids)
end

# User

default_scope { AccountUser.users }

many to many relation ship and reuse the default scope of child model to do it. don't forget unscoped of the original model

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 a pull request may close this issue.

4 participants