Rails and paperclip, delete the record but don't delete the attachment
you may want to take a look at how Attachment#assign
(called when you do object.attachment = new_attachment
) is implemented in paperclip.
Basically, it makes a bit of setup, then calls Attachment#clear
, then it saves the new file.
Attachment#clear puts the old file in a deletion queue that is processed when you call save again, what you want is simply to avoid the call to clear, which you could do by either writing a new assign method which skips that line or by monkey patching #clear
so that it becomes a no-op. In theory you could just monkey patch it on the instances where you want this to happen, but it seems to me you may want to do it for the whole project.
Or you can clear the instance variable holding the processing queue. That variable does not have an accessor, but it should be trivial to do an instance_variable_get
The gem allows you to do a soft delete:
(from paperclip)
File Preservation for Soft-Delete
An option is available to preserve attachments in order to play nicely with soft-deleted models. (acts_as_paranoid, paranoia, etc.)
has_attached_file :some_attachment, {
preserve_files: true,
}
This will prevent some_attachment from being wiped out when the model gets destroyed, so it will still exist when the object is restored later.