Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep paperclip from deleting attachments from an acts_as_paranoid model?

I have a model with a couple of different image attachments managed by paperclip. Recently we changed the behavior of the model so it could be soft-deleted and revived using acts_as_paranoid. Everything works great except that when we soft delete the model, paperclip is deleting the attachments as well.

I've looked through the paperclip docs and the code and I don't see an obvious way to circumvent this. I'd like to be able to tell paperclip to ignore the delete callbacks on the model and keep the attachments around?

like image 678
Jeff Whitmire Avatar asked Feb 11 '11 20:02

Jeff Whitmire


2 Answers

Paperclip now has a preserve_files option. Override shouldn't be necessary now.

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.

https://github.com/thoughtbot/paperclip#file-preservation-for-soft-delete

like image 117
Mark Nadig Avatar answered Oct 08 '22 11:10

Mark Nadig


Crazy how many times you find the answer to your own question right after you ask it. I'm dropping it here in case anybody else has the same issue, or maybe somebody has a better solution to this. What worked for me was to override the method paperclip uses to respond to the before_destroy callback. I dropped this into my code and it now preserves my attachments so they are there if I undelete the model later.

 protected  

  def destroy_attached_files
    logger.error "-------------- This is me NOT destroying my attachments"
  end
like image 34
Jeff Whitmire Avatar answered Oct 08 '22 10:10

Jeff Whitmire