I am trying to move files around my S3 container using CarrierWave to reorganize the folder structure.
I came to an existing Rails application where all the images for the class are uploaded to a folder named /uploads . This causes problems when, if two users upload different images with the same name, the second image overwrites the first. To solve this problem, I want to reorganize the folders to place each image in its own directory according to the instance of the ActiveRecord object. We use CarrierWave to control file downloads.
The old bootloader code had the following method:
def store_dir "uploads" end
I changed the method to reflect my new file storage scheme:
def store_dir "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" end
This works great for new images, but breaks the URL for old images. Existing images report that their URL is inside the new folder immediately after changing the model, and the image files are still stored in /uploads .
> object.logo.store_dir => "uploads/object/logo/133"
This is not true. This object should report its logo to /uploads .
My solution is to write a script to move the image files, but I did not find the correct methods in CarrierWave to move the files. My script will look something like this:
MyClass.all.each |image| filename = file.name
What should I do in the third line of my script to move the file to a new location?
Omar
source share