Search code examples
ruby-on-railscarrierwaverails-migrations

How to rename a folder used for uploads in carrierwave when renaming models and migrating the db?


I'm trying to rename a model and the corresponding tables, it worked out fine. What about the upload folder? Used something like this that tied the uploader folder to the model class name. Now the class name has changed.

class CsvImportUploaderUploader < CarrierWave::Uploader::Base
  permissions 0755

  storage :file

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

Solution

  • You can just keep using the same model name. If you had a User model that would be converted from:

    User => user
    

    model.class.to_s.underscore just returns the class name itself as a string underscored which is "user" in this example.

    If you want to keep everything as it was just write it literally:

    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
    "uploads/user/#{mounted_as}/#{model.id}"
    

    If you don't care about the name, things will just be store in a different folder. If you have a User model and changed it to a Customer model it changes like this:

    "uploads/user/#{mounted_as}/#{model.id}"
    "uploads/customer/#{mounted_as}/#{model.id}"
    

    As you can see, they will just be store in a different folder. All your links should still keep working.