Search code examples
ruby-on-railsruby-on-rails-3carrierwavermagick

Set up different storage directories for different models with carrierwave


I have model1 and model2. I need to upload the images of the two models in two different directories. For now my image_uploader is:

class ImageUploader < CarrierWave::Uploader::Base

  include CarrierWave::RMagick

  storage :file

  def store_dir
    "uploads/images"
  end
end

The images of model2 should be stored in uploads/images2. How can I define this?


Solution

  • If the only thing different between your uploaders is the store_dir function, then you don't need to create another uploader (although you can). Inside your uploader, you have access to the model, so you could do something like:

    def store_dir
        if model.class==Model1
            "upload/images1"
        elsif model.class==Model2
            "upload/images2"
        else
            "upload/images"
        end
    end