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

霸气de小男生 提交于 2021-02-07 14:31:04

问题


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?


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/4973496/how-do-i-keep-paperclip-from-deleting-attachments-from-an-acts-as-paranoid-model

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!