{"id":31,"date":"2014-02-20T15:36:58","date_gmt":"2014-02-20T15:36:58","guid":{"rendered":"http:\/\/www.morematter.net\/?p=31"},"modified":"2014-02-20T15:43:56","modified_gmt":"2014-02-20T15:43:56","slug":"comparing-strings-to-potentially-null-values-in-rails","status":"publish","type":"post","link":"https:\/\/experimentalism.net\/morematter\/comparing-strings-to-potentially-null-values-in-rails\/","title":{"rendered":"Comparing strings to potentially null values in Rails"},"content":{"rendered":"<p>I&#8217;m fairly new to Rails, and Ruby, for that matter, and not yet sure of some basic things. I&#8217;ve read a bunch about how to test for (and not test for) nil or empty, and also about how to avoid that need &#8211; for instance, see this very useful post on\u00a0<a href=\"http:\/\/www.keenertech.com\/articles\/2011\/06\/11\/the-empty-string-code-smell-in-rails\">keenertech.com<\/a>.<\/p>\n<p>But, I can&#8217;t seem to find anyone addressing my particular issue, which is that I need to compare a db value &#8211; say, user.middle_name &#8211; with a string imported from a CSV file (using <a title=\"Roo\" href=\"https:\/\/github.com\/Empact\/roo\">Roo<\/a>); I want to know if the imported data differs from the db value, but if user.middle_name is null, and I try user.middle_name == file_row[&#8220;middle_name&#8221;], I get a value of false, because, of course, nil does not equal an empty string (and all fields are imported from the CSV as strings, not nils).<\/p>\n<p>In the world of SQL, I might say something like isnull(user.middle_name, &#8221;) = file_row.middle_name, but I don&#8217;t see anything like that in Ruby.\u00a0 I could also try user.middle_name.to_s == file_row[&#8220;middle_name&#8221;], which should be fine here, but if the field is a date, or number, or boolean, it may not match.<\/p>\n<p>So, my solution was as follows:<\/p>\n<pre>(user.middle_name || \"\") == file_row[\"middle_name\"]<\/pre>\n<p>This seems to work just fine, but I just wonder, is it the &#8220;right&#8221; way to do this? Am I missing something? If anyone cares to share some insight, or if this <em>is<\/em> right and someone finds it helpful &#8230; well, that&#8217;s what this post is for.<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m fairly new to Rails, and Ruby, for that matter, and not yet sure of some basic things. I&#8217;ve read a bunch about how to test for (and not test for) nil or empty, and also about how to avoid that need &#8211; for instance, see this very useful post on\u00a0keenertech.com. But, I can&#8217;t seem [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,12],"tags":[],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-database","category-ruby-on-rails"],"_links":{"self":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":4,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"predecessor-version":[{"id":36,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/posts\/31\/revisions\/36"}],"wp:attachment":[{"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/experimentalism.net\/morematter\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}