简体   繁体   中英

Rails Active Storage with multi-tenancy using apartment gem

I have a rails app with multi-tenancy implemented using apartment gem. I have a model called Report that is excluded from multi-tenancy, ie it is common to all tenants.

# 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. But since the Report model is tenant agnostic, I want the attached files to be tenant agnostic as well.

Is there any way to add active_storage tables in excluded models list/schema?

Is there any way to add active_storage tables in excluded models list/schema?

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.

so, i use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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