Carrierwave to store the full path

Is it possible to make CarrierWave store in the database the full path of the downloaded files instead of the file name and re-generate them with every call?

The reason I want this is to change the structure in which I store the files, without the already downloaded files disappearing until they are moved to their new locations.

+4
source share
2 answers

In your bootloader you have this structure:

class YourUploader < CarrierWave::Uploader::Base
  ...

  def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  ...
end

I think that if you change store_dirto the full path, it will do what you want.

+1
source

My workaround is to save the directory by a separate attribute:

class MyModel
  before_save do
    self.content_path ||= "uploads/my_model/contents/#{id}"
  end
end

Then your bootloader will look like this:

class YourUploader < CarrierWave::Uploader::Base
  ...

  def store_dir
    model.content_path
  end

  ...
end
+1

Source: https://habr.com/ru/post/1543524/


All Articles