繁体   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