I’m using rails and paperclip to save images, the usual way.
When a record with an attachment is destroyed, the attachment also gets deleted from the file system.
99% of the time this is the correct action, however there is a case where I need the attachment to remain in the system even though the db record is deleted.
I was wondering if anyone knew how to do this.
I’ve tried setting the attachment fields to nil via an update_attribute before destroying the record, but the update_attribute also delete the file.
One way would be to ignore all the callbacks, however some of the other call backs are needed and this seems a little too much. Anyone know any better ways…
Cheers.
you may want to take a look at how
Attachment#assign(called when you doobject.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
#clearso 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