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

Auto Insert tenant_id Problem when filtering with ActiveRecord asociation #267

Open
morieeeenyo opened this issue Jan 22, 2025 · 0 comments

Comments

@morieeeenyo
Copy link

morieeeenyo commented Jan 22, 2025

I have activerecord-multi-tenant with following code in Gemfile.
so that I have to use it with Rails7.2

gem 'activerecord-multi-tenant', git: 'https://github.com/citusdata/activerecord-multi-tenant.git', ref: '0ac43aa98334a29ed3e5b33e0bcc5ee281f97254'

but in this version activerecord relation like following does not work propery.

class Hoge < ApplicationRecord
    multi_tenant :tenant
    belongs_to :fuga
    belongs_to :tenant
end
class FishType < ApplicationRecord
    multi_tenant :tenant
    belongs_to :tenant
    has_many :hoges
end

with these association, in MultiTenant.without scope, I got following result

[5] pry(main)> Hoge.first.fuga
  Fuga Load (2.8ms)  SELECT "fugas".* FROM "fugas" WHERE "fugas"."tenant_id" = 1 AND "fugas"."id" = $1 LIMIT $2  [["id", 10605], ["LIMIT", 1]]
=> nil

expected result i guess

[5] pry(main)> Hoge.first.fuga
    SELECT "fugas".* FROM "fugas" WHERE "fugas"."id" = $1 LIMIT $2  [["id", 101], ["LIMIT", 1]

I would like to filter fugas without tenant_id clause.

In Rails6.1.7 and activerecord-multi-tenant(1.2.0), I can get expected result

How can I solve this problem?

@morieeeenyo morieeeenyo changed the title Auto Insert tenant_id filtering in asociation Auto Insert tenant_id Problem when filtering with ActiveRecord asociation Jan 22, 2025
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

No branches or pull requests

1 participant