繁体   English   中英

如何在Rails中基于范围的多租户应用程序中创建可跨公司的代理模型

[英]How do I create an Agent model that can spans across Companies in a scope based multi tenancy app in Rails

我已经构建了一个多租户应用程序,该应用程序具有属于Company UserReport ,如下所示(多租户应用程序使用company_id来定义其他所有内容-用户和报告)。

company.rb

class Company < ApplicationRecord
    has_many :reports
    has_many :users
end

user.rb

class User < ApplicationRecord
    belongs_to :company
    has_many :reports
end

report.rb

class Report < ApplicationRecord
    belongs_to :user
    belongs_to :company
end

现在,我希望添加一个Agency模型(带有agency_users ),该模型将使能够管理多个公司(并且该公司进行报告)。 该代理将需要能够从一家公司切换到另一家公司。

我将如何处理? 代理商有很多公司

class Agent < ApplicationRecord
    has_many :companies
end

我无法完全确定代理如何在company_ids之间切换以查看其负责的公司(其客户)的报告。

根据评论,这是一个可能有用的解决方案

class Agency < ApplicationRecord
  has_many :companies
  has_many :users
end

class Company < ApplicationRecord
    has_many :reports
    belongs_to :agency
end

class Users < ApplicationRecord
    has_many :reports
    belongs_to :agency
    enum role: [:agent]
end

现在,当用户登录到您的应用程序时,向他显示他当前控制的公司的下拉列表。 将此id存储在会话中,并用于查询数据:

current_user.reports.where(company_id: session[:company_id].to_i)

创建和删除它们同样如此。

但是,这不是最佳解决方案(我不确定您的意图是什么)。

我会采用一些更笼统的方法,例如每个公司的用户角色,而UserCompany拥有companyuserrole而不是用户级别的role (也许他是一家公司的代理人,但管理另一家公司;等等。)。 所有这些都取决于您真正需要做什么。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM