开发者

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 do开发者_JAVA技巧n'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?


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


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜