<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: David Aldridge</title>
    <description>The latest articles on DEV Community by David Aldridge (@slimdave).</description>
    <link>https://dev.to/slimdave</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F21987%2Fde05cfa3-fac2-40d7-ad7a-91cb138b3af8.jpeg</url>
      <title>DEV Community: David Aldridge</title>
      <link>https://dev.to/slimdave</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/slimdave"/>
    <language>en</language>
    <item>
      <title>DRYing up Rails Scopes and Instance Methods</title>
      <dc:creator>David Aldridge</dc:creator>
      <pubDate>Tue, 01 Dec 2020 08:43:24 +0000</pubDate>
      <link>https://dev.to/slimdave/drying-up-rails-scopes-and-instance-methods-2ceh</link>
      <guid>https://dev.to/slimdave/drying-up-rails-scopes-and-instance-methods-2ceh</guid>
      <description>&lt;p&gt;This is a small tip about &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself"&gt;DRYness&lt;/a&gt;, which is generally held to be a Good Thing.&lt;/p&gt;

&lt;p&gt;It is also a good Rails practice to &lt;a href="https://blog.carbonfive.com/rails-database-best-practices/"&gt;make generous use&lt;/a&gt; of &lt;a href="https://guides.rubyonrails.org/active_record_querying.html#scopes"&gt;scopes&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;And I notice that in some cases it is difficult to reconcile both of these fine practices.&lt;/p&gt;

&lt;p&gt;As a fairly trivial example, consider a scope that is implemented as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scope :active_non_defaults, -&amp;gt; {
  where(active: true, default: false)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fundamentally it defines a set of database records/instances with logic that is executed at the database layer.&lt;/p&gt;

&lt;p&gt;It is used to return to your system an appropriate set of records, and for each of the instances based on these records you can infer that the condition "is this active and non-default?" is true.&lt;/p&gt;

&lt;p&gt;If you had a single instance that you had retrieved through some other method, perhaps a &lt;code&gt;#find&lt;/code&gt;, and then wanted to test whether it met a &lt;code&gt;active_non_default?&lt;/code&gt; condition, how would you do that?&lt;/p&gt;

&lt;p&gt;Possible you would implement a question mark method on the instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def active_non_defaults?
  active? &amp;amp;&amp;amp; !default?
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this is fine, but is the code still DRY? Arguably not, as the system now implements the same logic twice, once to find a set of records in the database and once to test whether an instance is a member of that set.&lt;/p&gt;

&lt;p&gt;This happens to me all the time, and here is one way around it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# A scope. Nothing to see here, same as above
scope :active_non_defaults, -&amp;gt; {
  where(active: true, default: false)
}

# A new scope. Hello, what's going on here?
scope :is_this?, -&amp;gt;(thing) {
  raise "Was expecting a Thing" unless thing.is_a? Thing

  where(id: thing.id)
end

# An instance method that uses both of them.
def active_non_default?
  self.class.active_non_defaults.is_this?(self).exists?
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Or you might go a step further with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scope :includes_this?, -&amp;gt;(thing) {
  raise "Was expecting a Thing" unless thing.is_a? Thing

  where(id: thing.id).exists?
end

def active_non_default?
  self.class.active_non_defaults.includes_this?(self)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What does this do?&lt;/p&gt;

&lt;p&gt;It executes the query defined by the scope, adding in a new condition to say "just for this one row", and tacks on the ActiveRecord method &lt;code&gt;#exists?&lt;/code&gt; that transforms the logic into a test of whether such a row exists.&lt;/p&gt;

&lt;p&gt;Or in other words, "is this one row a member of the set defined by the scope's query, yes or no?".&lt;/p&gt;

&lt;p&gt;It's not a great example, as you usually don't want to be executing SQL if you can avoid it and here it is easy to avoid, but if the scope is complex (perhaps referencing other scopes, and perhaps running a query anyway) then it starts to make more sense:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;scope :deletable, -&amp;gt; {
  not_active.
    older_then_six_months.
    not_flagged_for_undeletable.
    has_no_outstanding_issues.
    approved_by_the_lawyers.
    has_a_backup.
    not_subject_to_deletion_laws.
    etc
}

def deletable?
  self.class.deletable.includes_this?(self)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Of course in some cases this will perform worse than a skilfully handcrafted instance method, but because of the cleverness of database query optimisers this can be very efficient under the right circumstances.&lt;/p&gt;

&lt;p&gt;Consider that the query executed by our refactored instance method tells the database that it is not interested in a large set of data. The query optimiser knows that the SQL generated by &lt;code&gt;where(id: thing.id)&lt;/code&gt;, &lt;code&gt;things.id = 354&lt;/code&gt; perhaps, is a primary key lookup of a single row, and it finds that to be a very attractive execution starting point.&lt;/p&gt;

&lt;p&gt;A good quality query optimiser is going to check that condition first (and we can infer that it is going to be true), and then look through all of the other conditions in the SQL and apply them in the order most likely to return the result set as quickly as possible.&lt;/p&gt;

&lt;p&gt;So if the scope &lt;code&gt;not_active&lt;/code&gt; is a quick test on the row, and &lt;code&gt;approved_by_the_lawyers&lt;/code&gt; requires a join to a view with three &lt;code&gt;union&lt;/code&gt;s and four correlated subqueries, then it's going to check the &lt;code&gt;not_active&lt;/code&gt; condition first. Maybe it gets lucky with that and never has to check with the lawyers, and now you have a 1.2ms test.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It is a long standing principle in database performance optimisation that combining multiple queries into a single query will perform better than the multiple queries executed individually.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You might have implemented your instance method more conventionally and in the most efficient order, of course, with something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def deletable?
  not_active? &amp;amp;&amp;amp;
    older_then_six_months? &amp;amp;&amp;amp;
    not_flagged_for_undeletable? &amp;amp;&amp;amp;
    has_no_outstanding_issues? &amp;amp;&amp;amp;
    approved_by_the_lawyers? &amp;amp;&amp;amp;
    has_a_backup? &amp;amp;&amp;amp;
    not_subject_to_deletion_laws? &amp;amp;&amp;amp;
    etc?
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;... but if three of those instance methods are implemented with queries then you will end up running all three of them on a deletable instance, instead of one.&lt;/p&gt;

&lt;p&gt;But of course this is not always the case and nor can this be used for an instance that is not yet created, or which has changed since it was last saved to the database.&lt;/p&gt;

&lt;p&gt;So I will be using this with discretion and consideration of many factors, and offer it to you as an option for your consideration.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>sql</category>
      <category>dry</category>
    </item>
    <item>
      <title>Avoid converting nil to zero in Ruby with this Quick Tip</title>
      <dc:creator>David Aldridge</dc:creator>
      <pubDate>Wed, 25 Nov 2020 10:46:03 +0000</pubDate>
      <link>https://dev.to/slimdave/avoid-converting-nil-to-zero-in-ruby-with-this-quick-tip-16c2</link>
      <guid>https://dev.to/slimdave/avoid-converting-nil-to-zero-in-ruby-with-this-quick-tip-16c2</guid>
      <description>&lt;p&gt;Converting values to an Integer, Float or BigDecimal (or even Rational) in Ruby sometimes comes with an unwelcome side effect: that &lt;code&gt;nil&lt;/code&gt; responds to &lt;code&gt;#to_i&lt;/code&gt;, &lt;code&gt;#to_f&lt;/code&gt;, &lt;code&gt;#to_d&lt;/code&gt;, or &lt;code&gt;#to_r&lt;/code&gt; by returning a class-appropriate zero value.&lt;/p&gt;

&lt;p&gt;Sometimes this is fine.&lt;/p&gt;

&lt;p&gt;When it isn't, here is a quick tip for avoiding it - use the &lt;a href="https://ruby-doc.org/core-2.6/doc/syntax/calling_methods_rdoc.html#label-Safe+navigation+operator"&gt;safe navigation operator&lt;/a&gt;, which does not send the method that follows it to nil.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;irb(main):001:0&amp;gt; nil.to_i
=&amp;gt; 0
irb(main):002:0&amp;gt; nil&amp;amp;.to_i
=&amp;gt; nil
irb(main):003:0&amp;gt; nil.to_f
=&amp;gt; 0.0
irb(main):004:0&amp;gt; nil&amp;amp;.to_f
=&amp;gt; nil
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's it. Happy navigating!&lt;/p&gt;

</description>
      <category>ruby</category>
    </item>
  </channel>
</rss>
