<?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: SakuraMilkTea</title>
    <description>The latest articles on DEV Community by SakuraMilkTea (@sakuramilktea).</description>
    <link>https://dev.to/sakuramilktea</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%2F1691562%2F7ef3f0e6-5aa3-4e98-9b44-3e05ada8ab24.jpeg</url>
      <title>DEV Community: SakuraMilkTea</title>
      <link>https://dev.to/sakuramilktea</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sakuramilktea"/>
    <language>en</language>
    <item>
      <title>.present? VS .any? VS .exists?</title>
      <dc:creator>SakuraMilkTea</dc:creator>
      <pubDate>Sun, 30 Jun 2024 15:13:49 +0000</pubDate>
      <link>https://dev.to/sakuramilktea/present-vs-any-vs-exists-34pp</link>
      <guid>https://dev.to/sakuramilktea/present-vs-any-vs-exists-34pp</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftb165na0a56kjd20spwr.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftb165na0a56kjd20spwr.jpg" alt="Image description" width="552" height="414"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Something I have a difficult time wrapping my head around as a newbie is how actually super important it is to care about query speeds.&lt;br&gt;
Different methods will have &lt;em&gt;vastly&lt;/em&gt; different query speeds, and it's therefore to my advantage to understand how they actually work, in order to use the one that will do what I want while keep everything running smooth.&lt;/p&gt;

&lt;p&gt;Here are three methods I've been using recently that kind of look the same, but run differently in the background and therefore have different uses!&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.present?&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[ "", " ", false, nil, [], {} ].any?(&amp;amp;:present?)
# =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one tests for an object's general falsiness. As per the documentation, "An object is present if it’s not &lt;code&gt;blank?&lt;/code&gt;. An object is blank if it’s false, empty, or a whitespace string."&lt;br&gt;
Very important to point out that &lt;code&gt;.present?&lt;/code&gt; initializes ActiveRecord for each record found(!). It effectively goes through the whole relevant database available.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.any?&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;person.pets.count # =&amp;gt; 0
person.pets.any?  # =&amp;gt; false
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one will use a SQLCount to see if it's &amp;gt; 0, and therefore, although faster than &lt;code&gt;.present?&lt;/code&gt;, still takes time and queries the database multiple times.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;.exists?&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Note.create(:title =&amp;gt; 'Hello, world.', :body =&amp;gt; 'Nothing more for now...')
Note.exists?(1) # =&amp;gt; true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This one, according to the documentation, "asserts the existence of a resource, returning true if the resource is found."&lt;br&gt;
The beauty of &lt;code&gt;.exists?&lt;/code&gt; is that it uses SQL LIMIT 1 to just check if there's at least one record, without loading them all or counting them all!&lt;br&gt;
In a case where I just want to know if an object is there or not, this is without a doubt the best option.&lt;/p&gt;

&lt;p&gt;And now to remember these three...&lt;/p&gt;

&lt;p&gt;Let's do our best!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>database</category>
      <category>queries</category>
      <category>sql</category>
    </item>
    <item>
      <title>Interesting Things I learned Writing Rspec Tests</title>
      <dc:creator>SakuraMilkTea</dc:creator>
      <pubDate>Thu, 27 Jun 2024 14:24:35 +0000</pubDate>
      <link>https://dev.to/sakuramilktea/interesting-things-i-learned-writing-rspec-tests-3o4n</link>
      <guid>https://dev.to/sakuramilktea/interesting-things-i-learned-writing-rspec-tests-3o4n</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcekt5apxmw8ytbssdby1.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcekt5apxmw8ytbssdby1.jpg" alt="Image description" width="400" height="300"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Writing test code is good form and is obviously always recommended. Writing GOOD test is an excellent way to fool-proof the code you (I) have so far, but like everything else, it takes practice!&lt;/p&gt;

&lt;p&gt;In an attempt to keep it fresh in my mind, here are a few things I found out recently about writing rspec code...&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;travel_to&lt;/code&gt; is useful... But only allowed once.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Okay. That was a bit dramatic. You can actually use &lt;code&gt;travel_to&lt;/code&gt; in separate blocks as much as you want.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context "first context" do
  travel_to("2024-12-25") do
    expect(presents)
  end
end

context "second context" do
  travel_to("2024-12-31") do
    expect(fireworks)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, you're (I am) not allowed to do some magical shenanigans such as this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context "first context" do
  travel_to("2024-12-25") do
    expect(presents)

    travel_to("2024-12-31") do
      expect(fireworks)
    end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Not only does it look weird and messy, but also you'll get a big angry error. So yeah.&lt;br&gt;
However in some cases it will be inevitable for the whole file to be enclosed in a &lt;code&gt;travel_to&lt;/code&gt; for some reason or other. And that's when it's a good idea to use &lt;code&gt;travel_back&lt;/code&gt;!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context "first context" do
  travel_to("2024-12-25") do
    expect(presents)

  context "second context" do
    before do
      travel_back
    end
    travel_to("2024-12-31") do
      expect(fireworks)
    end
  end
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Simple? Yes. But does it do a good job? Also yes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update:&lt;/strong&gt; I have learned an even better method today. The trick is simply to not make subsequent &lt;code&gt;travel_to&lt;/code&gt; into blocks!&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;context "first context" do
  travel_to("2024-12-25") do
    expect(presents)

    travel_to("2024-12-31")
    expect(fireworks)
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;It's better to not write DSLs dynamically&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This one might come as a surprise for some, because it sure did for me. Consider this example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe 'some spec' do
  (1..7).each do |n|
    let!(:"user_#{n}") { create(:user, name: "user#{n}") } 
  end
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I thought I was super clever to use iteration like that, but learned from reviewers that it's not good practice. Although it saves time and works fine, it's not reader-friendly.&lt;br&gt;
Instead, a very similar approach can be used through FactoryBot&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe 'some spec' do
  let!(:users) { FactoryBot.create_list(:user, 7) } 
  # ...
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This obviously assumes that the name is set in the FactoryBot file&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;FactoryBot.define do
  factory :user do
    sequence(:name) { |n| "user#{n}" }
    # ...
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As I become more acquainted with rspec and keep writing more code, I might come back and edit this document with more interesting finds ☺︎&lt;/p&gt;

&lt;p&gt;Let's keep doing our best!&lt;/p&gt;

</description>
      <category>rspec</category>
      <category>testcode</category>
      <category>beginners</category>
      <category>rails</category>
    </item>
    <item>
      <title>A newbie's look at N+1 queries</title>
      <dc:creator>SakuraMilkTea</dc:creator>
      <pubDate>Thu, 27 Jun 2024 12:41:56 +0000</pubDate>
      <link>https://dev.to/sakuramilktea/a-newbies-look-at-n1-queries-2hl0</link>
      <guid>https://dev.to/sakuramilktea/a-newbies-look-at-n1-queries-2hl0</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea4oq071ub0z7yw6xkj5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fea4oq071ub0z7yw6xkj5.jpg" alt="Image description" width="600" height="408"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;N+1 queries usually happen when there is a one-to-many relationship, where we (I) try to call a record related to an object that is the child(many) of the main parent(one) object.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Let's use for this article an Author(one) and her books(many).&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you're not sure whether it IS an N+1 problem query or not, check your log (bin/dev).&lt;br&gt;
You'll know you have an N+1 error when your parent object is loaded fine, but then underneath you'll have a bunch of children object loaded separately from one another, back-to-back. &lt;/p&gt;

&lt;p&gt;So in our example, you'd see first that you'd be loading the author.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT "author" FROM "authors" WHERE "authors"."id" IN "Fantasy" AND "books"."published blablabla"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's fine, but then the problem arises when you see something like this after.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT "book" FROM "books" WHERE "books"."id" blablabla ["id", 4]
SELECT "book" FROM "books" WHERE "books"."id" blablabla ["id", 7]
SELECT "book" FROM "books" WHERE "books"."id" blablabla ["id", 9]
SELECT "book" FROM "books" WHERE "books"."id" blablabla ["id", 11]
SELECT "book" FROM "books" WHERE "books"."id" blablabla ["id", 15]
...etc
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So over and over again we're loading these books, and each time we do that, we're making a request to the database. Sure, that's not a big deal in my example because we made 5 requests, but it does start to become an issue when authors or author groups have hundreds of books. Publishing houses that have tens of thousands. Obviously this wouldn't be a good way to go about getting that data.&lt;/p&gt;

&lt;p&gt;The solution to this lies in the controller, where we are selecting the data, and on how we &lt;em&gt;include&lt;/em&gt; the author data in our book query... See what I did there?&lt;/p&gt;

&lt;p&gt;We are telling Rails that we want to run just one statement to select all that related data at once, instead of running multiple individual statements for each individual record later.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@books = Book.includes(:author).order(published_at: :desc)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looking back at our log, we should see something like this&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT "book" FROM "books" WHERE "books"."id" blablabla [["id", 4], ["id", 7], ["id", 9], ["id", 11], ["id", 15]]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's really good form to go back in your logs and look at how your data is being loaded, and where the load is coming from in your application's code! ☺︎ &lt;/p&gt;

&lt;p&gt;And for when we need a little extra help, there are always neat gems like &lt;a href="https://github.com/DmitryTsepelev/ar_lazy_preload"&gt;this one&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Let's keep doing our best!&lt;/p&gt;

</description>
      <category>nplus1</category>
      <category>beginners</category>
      <category>database</category>
      <category>queries</category>
    </item>
  </channel>
</rss>
