Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Carrierwave to store the full path

Is it possible to make CarrierWave store, in the database, the full path of the uploaded files instead of just the file name and re-generate them every time they are accessed?

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

like image 391
pupeno Avatar asked Jun 06 '14 08:06

pupeno


1 Answers

My workaround is to store the directory on a separate attribute:

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

Then your uploader will look like this:

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

  def store_dir
    model.content_path
  end

  ...
end
like image 117
Fábio Batista Avatar answered Sep 30 '22 06:09

Fábio Batista