Skip to content

CarrierWave and multiple databases

revis0r edited this page May 16, 2012 · 3 revisions

If you are use 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