{"id":2492,"date":"2009-11-10T19:56:09","date_gmt":"2009-11-10T23:56:09","guid":{"rendered":"http:\/\/blogs.n1zyy.com\/n1zyy\/?p=2492"},"modified":"2009-11-10T19:56:09","modified_gmt":"2009-11-10T23:56:09","slug":"keeping-originals-after-a-delete-in-paperclip","status":"publish","type":"post","link":"https:\/\/blogs.n1zyy.com\/n1zyy\/2009\/11\/10\/keeping-originals-after-a-delete-in-paperclip\/","title":{"rendered":"Keeping Originals after a Delete in Paperclip"},"content":{"rendered":"<p>This is almost certainly not of interest to the non-technical regulars here, but I&#8217;m hoping it&#8217;ll save someone down the road some time.<\/p>\n<p>At work I was having a hard time with what should have been a simple problem: we use <a href=\"http:\/\/github.com\/thoughtbot\/paperclip\">paperclip<\/a> for image uploads, and create a corresponding Photo object in the database. It is <em>not<\/em> necessary to have a database backing paperclip uploads in general, but in our case, it was. (In fact, with some polymorphism, too. Many things have Photos.)<\/p>\n<p>The problem is that paperclip has a feature that&#8217;s useful in 99% of cases: if you delete a Photo record (or whatever object has_attached_file), it will delete the object off of disk. This makes a lot of sense: if you tell the database to delete the reference to a photo, you probably want to get rid of the disk. The problem is that I did not. We wanted to preserve (&#8220;orphan,&#8221; a purist might argue) the image \/ &#8220;attached&#8221; file on disk even when you called Object.destroy.<\/p>\n<p>After a while of trying to figure out how to do this in paperclip, I finally found <a href=\"http:\/\/groups.google.com\/group\/paperclip-plugin\/browse_thread\/thread\/50fffff12d4010ad\">this discussion<\/a>. The answer, it turns out, is that you hack in. In the object that has the attachment (Photo for me), you override paperclip&#8217;s destroy_attached_files and just have it return true. Your neutered version takes precedence of paperclip&#8217;s, and the photos sit on disk with no reference in the database.<\/p>\n<p>Incidentally, I&#8217;ve found that I&#8217;m a grumpy curmudgeon who hates tools that &#8220;just work,&#8221; largely because when they <em>don&#8217;t<\/em> &#8220;just work&#8221; in some obscure situation, it&#8217;s a real pain to try to figure out why. People always say not to fight Rails. The thing I didn&#8217;t realize is that they don&#8217;t mean, &#8220;It will create a little extra work for you.&#8221; They mean, &#8220;You will want to change careers.&#8221;<\/p>","protected":false},"excerpt":{"rendered":"<p>This is almost certainly not of interest to the non-technical regulars here, but I&#8217;m hoping it&#8217;ll save someone down the road some time. At work I was having a hard time with what should have been a simple problem: we &hellip; <a href=\"https:\/\/blogs.n1zyy.com\/n1zyy\/2009\/11\/10\/keeping-originals-after-a-delete-in-paperclip\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2492","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/2492","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/comments?post=2492"}],"version-history":[{"count":0,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/posts\/2492\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/media?parent=2492"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/categories?post=2492"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.n1zyy.com\/n1zyy\/wp-json\/wp\/v2\/tags?post=2492"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}