Skip to content

CarrierWave and multiple databases

Andreas Wagner edited this page Feb 14, 2017 · 5 revisions

NOTE (16/9/2014) - There are very many reasons why you probably shouldn't do the below.

If you are using multiple databases in your Rails application, it's important to save files in different directories, for example named like subdomains. This is my example solution:

ApplicationController.rb

def override_db
  ...
  redefine_uploaders_store_dir
end

def redefine_uploaders_store_dir
  CarrierWave::Uploader::Base.descendants.each do |d|
    d.class_eval <<-RUBY, __FILE__, __LINE__+1
      def store_dir
        "uploads/#{@current_subdomain}/\#{model.class.to_s.underscore}/\#{mounted_as}/\#{model.id}"
      end
    RUBY
  end
end
Clone this wiki locally