{"id":2502,"date":"2017-12-03T10:53:48","date_gmt":"2017-12-03T16:53:48","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=2502"},"modified":"2017-12-03T10:53:48","modified_gmt":"2017-12-03T16:53:48","slug":"rails-gems-paper_trail","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/2502","title":{"rendered":"Rails Gems: paper_trail"},"content":{"rendered":"<p>Rails has an amazing number of quality libraries (called &#8216;gems&#8217;) and while building <a href=\"http:\/\/www.thefoodcorridor.com\/\">The Food Corridor application<\/a>, I was lucky enough to be able to use some of them.<\/p>\n<p>If you want to easily track changes in your rails models over time, the <a href=\"https:\/\/github.com\/airblade\/paper_trail\">paper_trail<\/a> gem has you covered.\u00a0 Simple to install, compatible with many databases, and very easy to use.\u00a0 I&#8217;d point you to the docs for installation, but for usage, all you have to do is drop <code>has_paper_trail<\/code> into your model class.<\/p>\n<p>After that&#8217;s deployed, every change to those models is tracked, including the entire model state.\u00a0 From an operational perspective, I&#8217;m a bit worried about the size of the audit table (called <code>versions<\/code>), but thus far it hasn&#8217;t grown too fast.\u00a0 It wouldn&#8217;t be hard to prune either.<\/p>\n<p>I use the data this gem records for two reasons.\u00a0 The primary purpose is to debug the system.\u00a0 When there&#8217;s an issue because the system doesn&#8217;t act as expected, or a question on when a model changed, I can go back and see how the model has changed over time.\u00a0 I can do this either by directly inspecting the <code>versions<\/code> table or via the rails console.\u00a0 If I use the latter, then I can actually pull back the model object as it existed in the past.\u00a0 This has been tremendously helpful in tracking down bugs.<\/p>\n<p>The secondary use of this data is to allow the system to see changes over time, and execute code based on those changes.\u00a0 This doesn&#8217;t happen often, and I&#8217;d prefer for such changes to be captured more explicitly, but in at least one case this gem has allowed me to fix an issue in a time efficient manner.<\/p>\n<p>If you&#8217;re building a complicated system that changes over time, as most real world applications do, having some kind of audit trail can be extremely helpful.\u00a0 <code>paper_trail<\/code> makes it trivial to get an audit trail set up in your rails app.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Rails has an amazing number of quality libraries (called &#8216;gems&#8217;) and while building The Food Corridor application, I was lucky enough to be able to use some of them. If [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[77,81,33],"tags":[],"class_list":["post-2502","post","type-post","status-publish","format-standard","hentry","category-rails","category-the-food-corridor","category-useful-tools"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2502","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2502"}],"version-history":[{"count":1,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2502\/revisions"}],"predecessor-version":[{"id":2503,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2502\/revisions\/2503"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2502"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2502"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2502"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}