{"id":336,"date":"2014-05-27T14:47:00","date_gmt":"2014-05-27T14:47:00","guid":{"rendered":"http:\/\/blogs.nd.edu\/devops\/?p=336"},"modified":"2014-05-27T14:52:24","modified_gmt":"2014-05-27T14:52:24","slug":"activerecord-psa-nil-vs-recordnotfound","status":"publish","type":"post","link":"https:\/\/sites.nd.edu\/devops\/2014\/05\/27\/activerecord-psa-nil-vs-recordnotfound\/","title":{"rendered":"ActiveRecord PSA: nil vs RecordNotFound"},"content":{"rendered":"<p>I&#8217;ve been meaning to get back into blogging here, and I think I have been blocked by the fact that many of the posts in my mental backlog are somewhat large in scope. \u00a0So here&#8217;s a useful bit of ActiveRecord trivia that I just learned.<\/p>\n<p>When no records are found, the &#8220;find&#8221; method (ie Person.find(&#8216;badinput&#8217;)) throws <strong>ActiveRecord::RecordNotFound<\/strong>.<\/p>\n<p>However, any find_by_* method, such as Person.find_by_netid(&#8216;badinput&#8217;), will return\u00a0<strong><em>nil<\/em><\/strong>.<\/p>\n<p>This was rather confusing as I focused on the error handling semantics of the Banner API. \u00a0<strong>I want that exception<\/strong>. \u00a0Good news, though:<\/p>\n<p>find_by_netid!(&#8216;badinput&#8217;) throws the exception. \u00a0The <strong>bang<\/strong> changes the behavior, as it often does, <a href=\"http:\/\/dablog.rubypal.com\/2007\/8\/15\/bang-methods-or-danger-will-rubyist\">though not always in the way you may expect<\/a>.<\/p>\n<p>TLDR:<\/p>\n<pre>2.0.0-p353 :001 &gt; Person.find('badinput')\r\nActiveRecord::RecordNotFound: Couldn't find Person with ndid=badinput &lt;snip&gt;\r\n\r\n2.0.0-p353 :002 &gt; Person.find_by_netid('badinput')\r\n =&gt; nil \r\n\r\n2.0.0-p353 :003 &gt; Person.find_by_netid!('badinput')\r\nActiveRecord::RecordNotFound: Couldn't find Person with netid = badinput &lt;snip&gt;\r\n\r\n<\/pre>\n<p>So that&#8217;s it. \u00a0Maybe this will get me back in the habit. \u00a0Happy coding!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been meaning to get back into blogging here, and I think I have been blocked by the fact that many of the posts in my mental backlog are somewhat large in scope. \u00a0So here&#8217;s a useful bit of ActiveRecord &hellip; <a href=\"https:\/\/sites.nd.edu\/devops\/2014\/05\/27\/activerecord-psa-nil-vs-recordnotfound\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1550,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-336","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/posts\/336","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/users\/1550"}],"replies":[{"embeddable":true,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/comments?post=336"}],"version-history":[{"count":5,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":341,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/posts\/336\/revisions\/341"}],"wp:attachment":[{"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sites.nd.edu\/devops\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}