How to Make Carrierwave Delete the File When Destroying a Record

How to make carrierwave delete the file when destroying a record?

Not sure what CarrierWave offers for this, but you could use FileUtils in the Ruby standard library with an ActiveRecord callback.

For instance,

require 'FileUtils'
before_destroy :remove_hard_image

def remove_hard_image
FileUtils.rm(path_to_image)
end

Sidenote: This code is from memory.

I would like to stop carrierwave deleting a file

Well AFAIK remove_previously_stored_files_after_update only work when a model object is updated so setting it to false will not remove the old file during update

But in your case you have to ensure the file are still present when the related model object is destroy

well I dont think there(if you examine the code here) is any mechanism currently available in in Carrierwave do that

but you can overwritten the remove! to achieve the same I guess this involve setting up attr_accessor (which is flag to decide whether to keep the file or delete it)

Inside your desired Model define a attr_accessor (say keep_file)

and in the desired uploader override the remove! method

class MyUploader < CarrierWave::Uploader::Base 
def remove!
unless model.keep_file
super
end
end
end

and ensure that you set the attr_accessor for the object (if you want to keep the deleted file) before destroying them

Example

u = User.find(10)
u.keep_file = true
u.destroy

This will ensure that file are cleaned up when the record is deleted from database

Let me know if there any better to do this

Hope this help

How do I get Carrierwave to delete a file from S3?

The problem is with your uploader. You are using secure random for store dir.

def store_dir
"files/#{SecureRandom.uuid()}"
end

When carrierwave try to delete file, Then due to random value store_dir point to some other place. I am sure you also not able to display this file anywhere.

So try something like this or something that is not randomly changes.

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

Carrierwave delete file from controller

Ok I finally found a solution myself. Here is my destroy method from AttachmentsController :

def destroy
model, param = get_attachable_instance
model_attach = model.find_by slug: params[param.to_sym]
file = model_attach.attachments.find_by slug: params[:id]
file.destroy

redirect_to :back
rescue ActionController::RedirectBackError
redirect_to root_path
end

Not sure if it's the best way to go, but it does works

Carrierwave file delete

You're calling the method on the wrong model. Your file mount is on the Attachment.

The error is telling you what is wrong.

undefined method 'remove_file' for #<Post:0x471a320

The key point of the error is the method is being called on the Post model when it needs to be called on the Attachment model.

Maybe try scoping the input for the checkbox to the correct model.

<%= attachment.check_box :remove_file %>

Trying to delete carrierwave folder

Your problem is that you don't instantiate any @agent variable on your agent model. If you want to get to the record attributes, you need to use self instead.
So change your remove_id_directory method to look something like this:

def remove_id_directory
FileUtils.rm_rf(File.join(Cw_storage_folder, 'agent', 'avatar', self.id)) if self.avatar
end


Related Topics



Leave a reply



Submit