<?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: Dario Chuquilla</title>
    <description>The latest articles on DEV Community by Dario Chuquilla (@dchuquilla).</description>
    <link>https://dev.to/dchuquilla</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%2F1251552%2Fe31454cc-6d69-4126-a6f3-4f4dacb8fe26.jpeg</url>
      <title>DEV Community: Dario Chuquilla</title>
      <link>https://dev.to/dchuquilla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dchuquilla"/>
    <language>en</language>
    <item>
      <title>Rails ActiveRecord count, size and length, map and pluck</title>
      <dc:creator>Dario Chuquilla</dc:creator>
      <pubDate>Fri, 15 Mar 2024 03:33:51 +0000</pubDate>
      <link>https://dev.to/dchuquilla/rails-activerecord-count-size-and-length-map-and-pluck-3ai3</link>
      <guid>https://dev.to/dchuquilla/rails-activerecord-count-size-and-length-map-and-pluck-3ai3</guid>
      <description>&lt;p&gt;Here I want to share with you some best practices for daily query requirements to help you improve the server's memory usage. We’ll discuss when to use each method and how they can help optimize memory usage and database calls.&lt;/p&gt;

&lt;h2&gt;
  
  
  Count and Size and length
&lt;/h2&gt;

&lt;p&gt;All of these are used to get the number of records retrieved by the query.&lt;/p&gt;

&lt;h3&gt;
  
  
  Count
&lt;/h3&gt;

&lt;p&gt;It returns the number of records by performing a query to the database every time it is called. I think it is pretty useful in asynchronous executions to bet the latest count of queries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT "users".* FROM "users"&lt;/span&gt;
&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;count&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT COUNT(*) FROM users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Size
&lt;/h3&gt;

&lt;p&gt;This is a bit smarter, it calls to &lt;code&gt;count&lt;/code&gt; in the database when records are not loaded in memory yet.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT "users".* FROM "users"&lt;/span&gt;
&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;
&lt;span class="c1"&gt;# No query needed, returns the number of records in memory&lt;/span&gt;
&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT COUNT(*) FROM users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Length
&lt;/h3&gt;

&lt;p&gt;This always gets from memory,  It’s similar to size method, but it always loads the records.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT "users".* FROM "users"&lt;/span&gt;
&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;
&lt;span class="c1"&gt;# No query needed, returns the number of records in memory&lt;/span&gt;
&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;
&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;*&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  A small recap
&lt;/h3&gt;

&lt;p&gt;You will see how &lt;code&gt;size&lt;/code&gt; and &lt;code&gt;length&lt;/code&gt; queries to database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;size&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT COUNT(*) FROM users&lt;/span&gt;
&lt;span class="c1"&gt;# Performs COUNT and saves that number into memory&lt;/span&gt;

&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;length&lt;/span&gt;
&lt;span class="no"&gt;SELECT&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;*&lt;/span&gt; &lt;span class="no"&gt;FROM&lt;/span&gt; &lt;span class="s2"&gt;"users"&lt;/span&gt;
&lt;span class="c1"&gt;# Performs a full scan query, load that result into memory, and then gets the length of that array.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Map and Pluck
&lt;/h2&gt;

&lt;p&gt;To get an array of certain fields of query results.&lt;/p&gt;

&lt;h3&gt;
  
  
  Map
&lt;/h3&gt;

&lt;p&gt;This method performs over the array of results of query and it only works with one field at a time, for more fields, you can pass a bloq as an argument, but first, it will load all fields of all records in memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT "users".* FROM "users"&lt;/span&gt;
&lt;span class="c1"&gt;# [1, 2, 3, ...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Pluck
&lt;/h3&gt;

&lt;p&gt;This is a more efficient method than &lt;code&gt;map&lt;/code&gt;, it performs a query with the specific field you want to get, so It consumes less memory and traffic with DB.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="no"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pluck&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:first_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;# SELECT "users"."id", "users"."first_name" FROM "users"&lt;/span&gt;
&lt;span class="c1"&gt;# [[1, "name 001"], [2, "name 002"], [3, "name 003"], ...]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;All of these methods are useful for all our requirements, sometimes it's better to load all info in memory at the beginning of the whole process (in tables with a small number of records), and it is necessary to control what you get from DB mostly in tables with a huge number of records.&lt;/p&gt;

&lt;p&gt;For counting I prefer to use &lt;code&gt;size&lt;/code&gt; to not call DB every time I need the number of records in a simple request flow. For a small number of fields 'pluck' is your friend.&lt;/p&gt;

&lt;p&gt;Remember, optimizing database calls and memory usage is crucial for maintaining a fast, responsive Rails application. Always consider the trade-offs when choosing which ActiveRecord method to use.&lt;/p&gt;

&lt;p&gt;I hope this helps! Let me know if you need any further assistance.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>activerecord</category>
      <category>database</category>
    </item>
    <item>
      <title>Ruby Attributes VS Instance variables</title>
      <dc:creator>Dario Chuquilla</dc:creator>
      <pubDate>Mon, 04 Mar 2024 02:02:52 +0000</pubDate>
      <link>https://dev.to/dchuquilla/ruby-attributes-vs-instance-variables-d05</link>
      <guid>https://dev.to/dchuquilla/ruby-attributes-vs-instance-variables-d05</guid>
      <description>&lt;p&gt;I am finally going to &lt;strong&gt;clarify&lt;/strong&gt; the confusion among developers about the use of &lt;strong&gt;attributes and instance variables&lt;/strong&gt; in Ruby classes. In this article, I will show you the similarities and differences between these two concepts and provide &lt;strong&gt;best practices for using them&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  First things first SIMILARITIES
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Attributes and instance variables are used to store data &lt;strong&gt;within a class&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;They can both be accessed and modified &lt;strong&gt;within the class&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Now the differences
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Attributes have a &lt;strong&gt;wider scope&lt;/strong&gt; than instance variables. &lt;/li&gt;
&lt;li&gt;Attributes can be accessed and modified &lt;strong&gt;outside the class&lt;/strong&gt; using accessors, while instance variables are only accessible within the class.&lt;/li&gt;
&lt;li&gt;Attributes can be defined with &lt;strong&gt;specific data types&lt;/strong&gt;, ensuring data integrity. Instance variables, on the other hand, can hold &lt;strong&gt;any data type&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Attributes can be initialized &lt;strong&gt;during declaration&lt;/strong&gt;, while instance variables &lt;strong&gt;need&lt;/strong&gt; to be initialized &lt;strong&gt;within the class&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;Instance variables consume &lt;em&gt;more memory&lt;/em&gt; than attributes since they are not bound to a specific data type.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best practices
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;attr_accessor&lt;/strong&gt; is so versatile, that it lets us to store a value anywhere.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AttributesInstances&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:my_attribute&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
    &lt;span class="vi"&gt;@my_attribute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &lt;span class="c1"&gt;# instance variable used to store the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_me&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@my_attribute&lt;/span&gt; &lt;span class="c1"&gt;# instance variable used to access the value of the attribute&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_attribute&lt;/span&gt; &lt;span class="c1"&gt;# attribute reader used to access the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AttributesInstances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_me&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, it is possible to access the attribute value by the instance variable used to store it during the initialization.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AttributesInstances&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:my_attribute&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
    &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_attribute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;"Hello"&lt;/span&gt; &lt;span class="c1"&gt;# attribute writer used to set the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_me&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@my_attribute&lt;/span&gt; &lt;span class="c1"&gt;# instance variable used to access the value of the attribute&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_attribute&lt;/span&gt; &lt;span class="c1"&gt;# attribute reader used to access the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AttributesInstances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_me&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this case, the value can be stored with &lt;code&gt;self&lt;/code&gt; which is the attribute writer method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;attr_reader and attr_writer&lt;/strong&gt; It has their special treatment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AttributesInstances&lt;/span&gt;
  &lt;span class="nb"&gt;attr_reader&lt;/span&gt; &lt;span class="ss"&gt;:my_attribute_reader&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;
    &lt;span class="c1"&gt;# self.my_attribute_reader = 'Hello' # RISES NoMethodError: undefined method `my_attribute_reader=' for #&amp;lt;AttributesInstances:0x00007f8f3b8b3e40&amp;gt;&lt;/span&gt;
    &lt;span class="vi"&gt;@my_attribute_reader&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Hello'&lt;/span&gt; &lt;span class="c1"&gt;# instance variable used to set the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;show_me&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="vi"&gt;@my_attribute_reader&lt;/span&gt; &lt;span class="c1"&gt;# instance variable used to access the value of the attribute&lt;/span&gt;
    &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;my_attribute_reader&lt;/span&gt; &lt;span class="c1"&gt;# attribute reader used to access the value of the attribute&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;ai&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;AttributesInstances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;
&lt;span class="n"&gt;ai&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;show_me&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;span class="c1"&gt;#=&amp;gt; Hello&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the value can only be stored by an instance variable, when &lt;code&gt;self&lt;/code&gt; is used, it raises the &lt;code&gt;undefined method&lt;/code&gt; exception.&lt;/p&gt;

&lt;p&gt;I hope this is useful for getting your code cleaner and better organized.&lt;/p&gt;

&lt;p&gt;Any comments are fully welcomed.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>oop</category>
      <category>programming</category>
      <category>certification</category>
    </item>
  </channel>
</rss>
