{"id":2231,"date":"2016-08-04T21:23:14","date_gmt":"2016-08-05T03:23:14","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=2231"},"modified":"2016-08-04T21:23:14","modified_gmt":"2016-08-05T03:23:14","slug":"perils-of-orm-caching","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/2231","title":{"rendered":"Perils of ORM caching"},"content":{"rendered":"<p>So, I was working a rails4 project today and added an <code>after_create<\/code> method to a model (call it model A) that checked on a related object (call it model B) to see its state, and if it met a certain criteria, did something to the model A object being created.  The specifics don&#8217;t really matter, but I was using <a href=\"https:\/\/github.com\/burke\/zeus\">zeus<\/a> to run my rpsec tests.<\/p>\n<p>This caused three tests to fail in entirely unrelated sections of the application.  <\/p>\n<p>What on earth was going on?<\/p>\n<p>Well, first I used <a href=\"https:\/\/git-scm.com\/docs\/git-bisect\"><code>git bisect<\/code><\/a> to determine the exact commit that caused the issue.\u00a0 (As far as I&#8217;m concerned, the existence of <code>git bisect<\/code> confirms my belief to &#8216;commit early, commit often&#8217;).<\/p>\n<p>Then I dug in.\u00a0 It appears that each of the tests was tweaking the model B object, and testing some aspect of the change, usually through the model A object.\u00a0 Before I added the <code>after_create<\/code> method, the model B object wasn&#8217;t loaded into the ActiveRecord in memory network graph tied to the model A object when the test saved the model A object initially, but was loaded from the database when the method under test executed.<\/p>\n<p>After the <code>after_create<\/code> method was added, the model B object was loaded into the in memory network graph tied to the model A object.\u00a0 Then the test tweaked the model B object in the database, but didn&#8217;t reload the model A object, which had a dirty\/old version of the model B object.<\/p>\n<p>A simple reload of model A (and its network graph) fixed it (or a repositioning of when I modified the model B object), but it was quite a subtle testing bug to track down.  <\/p>\n","protected":false},"excerpt":{"rendered":"<p>So, I was working a rails4 project today and added an after_create method to a model (call it model A) that checked on a related object (call it model B) [&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,58],"tags":[],"class_list":["post-2231","post","type-post","status-publish","format-standard","hentry","category-rails","category-testing"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2231","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=2231"}],"version-history":[{"count":1,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2231\/revisions"}],"predecessor-version":[{"id":2232,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/2231\/revisions\/2232"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2231"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2231"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2231"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}