繁体   English   中英

使用公寓 gem 的多租户 Rails Active Storage

[英]Rails Active Storage with multi-tenancy using apartment gem

我有一个使用公寓 gem 实现的多租户 Rails 应用程序。 我有一个名为 Report 的 model 被排除在多租户之外,也就是说,它对所有租户都是通用的。

# app/models/report.rb
class Report < ApplicationRecord
  has_one_attached :file
  ...
end
# config/initializers/apartment.rb
Apartment.configure do |config|
  config.excluded_models = %w{ Tenant User Report }
  ...
end

在报告控制器中:

  ...

  def upload
    @record = Report.find(params[:report_id])
    record.file.attach(params[:file])
    head 200
  end

  ...

当我上传文件时,active_storage 仅更新对当前租户有效的架构。 但由于报告 model 与租户无关,我希望附件也与租户无关。

有没有办法在排除模型列表/模式中添加 active_storage 表?

有没有办法在排除模型列表/模式中添加 active_storage 表?

不,我想。

如果在排除模型列表中添加“ActiveStorage::Attachment”和“ActiveStorage::Blob”,所有文件数据都将保存在公共(非租户)数据库中。

所以,我使用 Apartment::Tenant.switch。

在你的情况下是这样的:

  def upload
    Apartment::Tenant.switch('db name you want to use') do
      @record = Report.find(params[:report_id])
      record.file.attach(params[:file])
      head 200
    end
  end

暂无
暂无

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

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