How to get a paper clip to delete files

I have a model using Paperclip to manage a file.

After I delete the model, I obviously would like the file to be deleted as well, but I cannot figure out how to delete the file using Paperclip.

I tried self.sourcefile = nil if !sourcefile.dirty? in before_destroy def, but this did not affect.

(I want it to delete the file locally when I test, and then on S3 when I use it - so I need a clean clip solution)

Any ideas?

+4
source share
2 answers

The clip is very noisy when you cannot delete the file.

Without any special configuration, when an object is deleted, related files, if any, are also deleted, and this is noted in the log (at least in development mode):

 [paperclip] Deleting attachments. [paperclip] deleting /clipper/public/system/avatars/3/original/whatever.JPG 

If Paperclip cannot delete the file, it simply throws an exception.

All this has been tested with the latest Paperclip (cloned from github on May 25th). If you have an older version installed, you can upgrade it, and then run some tests while viewing the logs.

+10
source

Maybe I'm wrong, but I think he should do it automatically, since has_attached_file adds a before_destroy . Source

Edit: if it doesn't work, maybe you can try calling destroy_attached_files yourself, which would seem strange to me. Source

0
source

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


All Articles