簡體   English   中英

Rails 4 ActsAsTenant復合體has_many through與設置的current_tenant不兼容

[英]Rails 4 ActsAsTenant complex has_many through doesn't work with setted current_tenant

我的多租戶應用中有以下模型:

class Tenant < ActiveRecord::Base
  has_many :userroles
  has_many :users, through: :userroles
  has_many :roles, through: :userroles
  has_many :admins, -> { joins(:roles).where("roles.name = 'admin'").uniq }, through: :userroles, class_name: 'User', source: :user
end

class Role < ActiveRecord::Base
  has_paper_trail
  acts_as_paranoid
  has_many :userroles, :dependent => :destroy
  has_many :users, :through => :userroles
end

class Userrole < ActiveRecord::Base
  acts_as_tenant(:tenant)
  has_paper_trail
  belongs_to :user
  belongs_to :role
end

我使用Erwin編寫的gem ActsAsTenant(github上的源代碼)。 當current_tenant不能正確設置我的代碼時,但是如果設置current_tenant,則會出錯。 在控制台中,我得到了這些錯誤:

2.1.0 :001 > ActsAsTenant.current_tenant
 => nil 
2.1.0 :002 > t = Tenant.first
2.1.0 :004 > t.admins.count
   (1.5ms)  SELECT DISTINCT COUNT(DISTINCT "users"."id") FROM "users" INNER JOIN "userroles" "userroles_users_join" ON "userroles_users_join"."user_id" = "users"."id" INNER JOIN "roles" ON "roles"."id" = "userroles_users_join"."role_id" AND "roles"."deleted_at" IS NULL INNER JOIN "userroles" ON "users"."id" = "userroles"."user_id" WHERE "users"."deleted_at" IS NULL AND "userroles"."tenant_id" = $1 AND (roles.name = 'admin')  [["tenant_id", 1]]
 => 1 
2.1.0 :005 > ActsAsTenant.current_tenant = t
2.1.0 :006 > t.admins.count
   (2.6ms)  SELECT DISTINCT COUNT(DISTINCT "users"."id") FROM "users" INNER JOIN "userroles" "userroles_users_join" ON "userroles_users_join"."user_id" = "users"."id" AND (userroles.tenant_id = 1) INNER JOIN "roles" ON "roles"."id" = "userroles_users_join"."role_id" AND "roles"."deleted_at" IS NULL INNER JOIN "userroles" ON "users"."id" = "userroles"."user_id" WHERE "users"."deleted_at" IS NULL AND "userroles"."tenant_id" = $1 AND (userroles.tenant_id = 1) AND (roles.name = 'admin')  [["tenant_id", 1]]
PG::UndefinedTable: ERROR:  invalid reference to FROM-clause entry for table "userroles"
LINE 1: ...erroles_users_join"."user_id" = "users"."id" AND (userroles....
                                                             ^
HINT:  Perhaps you meant to reference the table alias "userroles_users_join".
: SELECT DISTINCT COUNT(DISTINCT "users"."id") FROM "users" INNER JOIN "userroles" "userroles_users_join" ON "userroles_users_join"."user_id" = "users"."id" AND (userroles.tenant_id = 1) INNER JOIN "roles" ON "roles"."id" = "userroles_users_join"."role_id" AND "roles"."deleted_at" IS NULL INNER JOIN "userroles" ON "users"."id" = "userroles"."user_id" WHERE "users"."deleted_at" IS NULL AND "userroles"."tenant_id" = $1 AND (userroles.tenant_id = 1) AND (roles.name = 'admin')
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  invalid reference to FROM-clause entry for table "userroles"

問題是當我設置current_tenant時。 設置之前,所有操作都可以進行。 可能是什么問題呢? 在gem的代碼中,我找不到任何奇怪的東西。

我更改has_many條件:

has_many :admins, -> { for_tenanted_roles.where("roles.name = 'admin'").uniq }, through: :userroles, class_name: 'User', source: :user

並在user.rb

scope :for_tenanted_roles, -> { joins('INNER JOIN "roles" ON "roles"."id" = "userroles"."role_id" AND "roles"."deleted_at" IS NULL') }

這只是方便的重寫joins(:roles)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM