简体   繁体   English

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

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

I have a rails app with multi-tenancy implemented using apartment gem.我有一个使用公寓 gem 实现的多租户 Rails 应用程序。 I have a model called Report that is excluded from multi-tenancy, ie it is common to all tenants.我有一个名为 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

In ReportsController:在报告控制器中:

  ...

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

  ...

When I upload the file, active_storage updates only the schema that is valid for the current tenant.当我上传文件时,active_storage 仅更新对当前租户有效的架构。 But since the Report model is tenant agnostic, I want the attached files to be tenant agnostic as well.但由于报告 model 与租户无关,我希望附件也与租户无关。

Is there any way to add active_storage tables in excluded models list/schema?有没有办法在排除模型列表/模式中添加 active_storage 表?

Is there any way to add active_storage tables in excluded models list/schema?有没有办法在排除模型列表/模式中添加 active_storage 表?

no, i think.不,我想。

If you add 'ActiveStorage::Attachment' and 'ActiveStorage::Blob' in the exclude models list, all file data will be saved in the public(not tenant) db.如果在排除模型列表中添加“ActiveStorage::Attachment”和“ActiveStorage::Blob”,所有文件数据都将保存在公共(非租户)数据库中。

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

in your case like this:在你的情况下是这样的:

  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