<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Sikachu&#39;s Blog - Medium]]></title>
        <description><![CDATA[Prem Sichanugrist&#39;s blog featuring programming, web development, open source contribution, technology, and gaming. - Medium]]></description>
        <link>https://sikac.hu?source=rss----457e61dd5fd---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>Sikachu&amp;#39;s Blog - Medium</title>
            <link>https://sikac.hu?source=rss----457e61dd5fd---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 09 Jun 2026 00:44:43 GMT</lastBuildDate>
        <atom:link href="https://sikac.hu/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Ignore Git revisions in git blame]]></title>
            <link>https://sikac.hu/ignore-git-revisions-in-git-blame-1315d28fa31f?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/1315d28fa31f</guid>
            <category><![CDATA[programming-tips]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[github]]></category>
            <category><![CDATA[code-style]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Fri, 22 Jul 2022 07:55:56 GMT</pubDate>
            <atom:updated>2022-07-22T07:55:56.834Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Since Git 2.23, Git <a href="https://github.com/git/git/commit/ae3f36dea16e51041c56ba9ed6b38380c8421816">added support for ignoring revisions in </a><a href="https://github.com/git/git/commit/ae3f36dea16e51041c56ba9ed6b38380c8421816">git blame</a>. This feature is handy when you have a few revisions that only change code style or formatting and you want to exclude them for when you do git blame.</p><p>You can utilize this feature by calling git blame with <a href="https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revltrevgt">--ignore-rev or </a><a href="https://git-scm.com/docs/git-blame#Documentation/git-blame.txt---ignore-revltrevgt">--ignore-revs-file flag</a>, or <a href="https://git-scm.com/docs/git-config#Documentation/git-config.txt-blameignoreRevsFile">set </a><a href="https://git-scm.com/docs/git-config#Documentation/git-config.txt-blameignoreRevsFile">blame.ignoreRevsFile config</a>. There’s a good blog post by <a href="https://twitter.com/mheap">Micheal Heap</a> about this feature here:</p><p><a href="https://michaelheap.com/git-ignore-rev/">A better `git blame` with `--ignore-rev`</a></p><p>So, the reason I want to write about this feature despite it’s already available for a while now is that GitHub now officially supports it!</p><p><a href="https://github.blog/changelog/2022-03-24-ignore-commits-in-the-blame-view-beta/">Ignore commits in the blame view (Beta) | GitHub Changelog</a></p><p>You can now add a .git-blame-ignore-revs file to the root of your repository, and GitHub will now exclude those revisions from the blame view.</p><p>You can see an example usage of this feature on Ruby’s Git repository, where they use this feature to exclude a commit that <a href="https://github.com/ruby/ruby/commit/5b21e94bebed90180d8ff63dad03b8b948361089">changes the whitespaces of the whole project</a>.</p><p><a href="https://github.com/ruby/ruby/blob/55c771c302f94f1d1d95bf41b42459b4d2d1c337/.git-blame-ignore-revs">ruby/.git-blame-ignore-revs at 55c771c302f94f1d1d95bf41b42459b4d2d1c337 · ruby/ruby</a></p><p>If you ever thinking about cleaning up the code style of your repositories so they would be consistent, I think now is the right time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1315d28fa31f" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/ignore-git-revisions-in-git-blame-1315d28fa31f">Ignore Git revisions in git blame</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Confirm current database connection in Rails console]]></title>
            <link>https://sikac.hu/confirm-current-database-connection-in-rails-console-fd4ae68fc802?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/fd4ae68fc802</guid>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Fri, 04 Feb 2022 08:07:26 GMT</pubDate>
            <atom:updated>2022-02-04T08:10:20.588Z</atom:updated>
            <content:encoded><![CDATA[<p>If you are using <a href="https://guides.rubyonrails.org/active_record_multiple_databases.html">Rails muti-database feature</a>, it&#39;s quite useful to know if you are connecting to the main writer database or a read replica before performing a database query in the Rails console.</p><p>The easiest way to do this is to call <a href="https://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-connection_db_config">ActiveRecord::Base.connection_db_config</a> from the console:</p><pre>&gt; ActiveRecord::Base.connection_db_config<br>=&gt; <br>#&lt;ActiveRecord::DatabaseConfigurations::UrlConfig:0x123456<br> @configuration_hash={...},<br> @env_name=&quot;production&quot;,<br> @name=&quot;main&quot;,<br> @url=&quot;...&quot;&gt;</pre><p>You can see right away that we’re connecting to the main writer database.</p><p>If you want to switch your current database for the remaining of your session, you can use <a href="https://api.rubyonrails.org/classes/ActiveRecord/ConnectionHandling.html#method-i-connecting_to">ActiveRecord::Base.connecting_to</a>:</p><pre>&gt; ActiveRecord::Base.connecting_to(role: :reading)<br>=&gt; [{:role=&gt;:reading, :shard=&gt;:default, :prevent_writes=&gt;true, :klasses=&gt;[ActiveRecord::Base]}]<br>&gt; ActiveRecord::Base.connection_db_config.name<br>=&gt; &quot;reader&quot;</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fd4ae68fc802" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/confirm-current-database-connection-in-rails-console-fd4ae68fc802">Confirm current database connection in Rails console</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Vim modeline magic]]></title>
            <link>https://sikac.hu/vim-modeline-magic-663f6b71b67b?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/663f6b71b67b</guid>
            <category><![CDATA[vim]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Thu, 02 Apr 2020 05:34:41 GMT</pubDate>
            <atom:updated>2020-04-02T05:34:41.795Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>So I have this ~/.aliases file in my home directory that contain all of my aliases. However, everytime I open the file I have to do :set filetype=bash to make Vim syntax highlight my file … until today!</p><p>After a quick Googling for “vim magic comment”, I found this amazing tips:</p><p><a href="https://vim.fandom.com/wiki/Modeline_magic">Modeline magic</a></p><p>This is exactly what I wanted. So, I just added this on the top of my file:</p><pre># vim: set filetype=bash:</pre><p>And Voilà! Custom syntax highlight for this particular file. 💯</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=663f6b71b67b" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/vim-modeline-magic-663f6b71b67b">Vim modeline magic</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Writing a pass-through method, Ruby 2.7 edition]]></title>
            <link>https://sikac.hu/writing-a-pass-through-method-ruby-2-7-edition-a336239f2a61?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/a336239f2a61</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Thu, 26 Mar 2020 14:09:22 GMT</pubDate>
            <atom:updated>2020-03-26T14:09:22.203Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>You may have written a Ruby method which pass all the arguments to the underlying method. For example:</p><pre>def self.run(*args, &amp;block)<br>  new.run(*args, &amp;block)<br>end</pre><p>With the addition of keyword arguments in Ruby 2.0, things have gotten a bit crazier since you may want to pass all the keyword arguments to the underlying method as well. So, you’d want to update your method above to:</p><pre>def self.run(*args, **opts, &amp;block)<br>  new.run(*args, **opts, &amp;block)<br>end</pre><p>However, starting from Ruby 2.7, you no longer need to do that as you can now use the new “forward everything” syntax. So, you can now rewrite the code above to look like this:</p><pre>def self.run(...)<br>  new.run(...)<br>end</pre><p>… which is much cleaner than before!</p><p>If you would like to learn more about the development of this new syntax, you can look at the <a href="https://bugs.ruby-lang.org/issues/16253">Redmine issue</a>. Special thanks to <a href="https://github.com/byroot">Jean Boussier</a> for submitting <a href="https://github.com/rails/rails/pull/38023">this pull request</a> to Rails so I learned that this feature exist in Ruby 2.7.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a336239f2a61" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/writing-a-pass-through-method-ruby-2-7-edition-a336239f2a61">Writing a pass-through method, Ruby 2.7 edition</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[fetch vs. [] || for default value in Ruby]]></title>
            <link>https://sikac.hu/fetch-vs-bracket-or-for-default-value-in-ruby-9bfd3b4cc37e?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/9bfd3b4cc37e</guid>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[performance]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Sat, 23 Mar 2019 08:36:37 GMT</pubDate>
            <atom:updated>2022-12-07T15:31:56.260Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p><strong>TL;DR: </strong>Hash#fetch is now faster than doing [] || in Ruby 3.1+.</p><pre> &gt; RUBY_VERSION<br> =&gt; &quot;3.1.2&quot; <br> &gt; require &quot;benchmark/ips&quot;<br> =&gt; true <br> &gt; Benchmark.ips do |x|<br> &gt;   x.report(&quot;fetch&quot;) { {}.fetch(:non_existance, true) }<br> &gt;   x.report(&quot;[] ||&quot;) { {}[:non_existance] || true }<br> &gt; end; nil<br>Warming up --------------------------------------<br>               fetch     1.480M i/100ms<br>               [] ||     1.394M i/100ms<br>Calculating -------------------------------------<br>               fetch     14.812M (± 0.3%) i/s -     75.460M in   5.094490s<br>               [] ||     13.898M (± 0.9%) i/s -     69.715M in   5.016568s<br> =&gt; nil </pre><p>So, you should be fine using #fetch with default value anywhere now!</p><p><em>The original version of this blog post was written around Ruby 2.6 and stated that the only reason you want to use </em><em>#fetch is when you want your code to raise </em><em>KeyError when a key is not found. However, the Ruby Core team has since updated the performance of </em><em>Hash#fetch and fixed the problem. Hence, I updated this blog post to reflect the change.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=9bfd3b4cc37e" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/fetch-vs-bracket-or-for-default-value-in-ruby-9bfd3b4cc37e">fetch vs. [] || for default value in Ruby</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Missing translations keys — Gotta Catch ’Em All]]></title>
            <link>https://sikac.hu/missing-translations-keys-gotta-catch-em-all-3aa8d2ead8bd?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/3aa8d2ead8bd</guid>
            <category><![CDATA[internationalization]]></category>
            <category><![CDATA[i18n]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[web-development]]></category>
            <category><![CDATA[ruby-on-rails-development]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Tue, 23 Oct 2018 06:15:40 GMT</pubDate>
            <atom:updated>2018-10-23T06:15:40.335Z</atom:updated>
            <content:encoded><![CDATA[<h3>Missing translations keys — Gotta Catch ’Em All</h3><p>A few years back, Rails 4.1.0 introducedraise_on_missing_translations flag for Action View. Setting this key to true in your environment configuration will make Rails <a href="https://github.com/rails/rails/blob/v5.2.1/actionview/lib/action_view/base.rb#L159-L160">raises exception</a> if you have a missing translation keys:</p><pre># In config/environments/test.rb (and development.rb)<br>config.action_view.raise_on_missing_translations = true</pre><p>However, one thing that people may have missed is that this configuration <em>only applies to Rails views</em>. If you call I18n.translate or I18n.t directly from your specs, or when you build a JSON output, Rails won’t raise any exception at all.</p><p>So, instead, you will want to directly set an I18n exception handler like this:</p><pre># config/initializers/i18n.rb<br>if Rails.env.in?(%(development test))<br>  I18n.exception_handler = Proc.new do |exception, *_|<br>    raise exception.to_exception<br>  end<br>end</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3aa8d2ead8bd" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/missing-translations-keys-gotta-catch-em-all-3aa8d2ead8bd">Missing translations keys — Gotta Catch ’Em All</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Think twice before using double splat in Ruby]]></title>
            <link>https://sikac.hu/think-twice-before-using-double-splat-in-ruby-ee104fbb9cb2?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/ee104fbb9cb2</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Wed, 04 Jul 2018 01:17:13 GMT</pubDate>
            <atom:updated>2018-07-04T01:17:13.036Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>As of Ruby 2.5.1, double splat operator (**options, for example) is still considerably slower than accepting a normal argument and sets a default to empty hash.</p><p>Here’s a simple benchmark:</p><pre>require &quot;benchmark/ips&quot;<br><br>def option_hash(options = {}); end<br>def double_splat(**options); end<br><br>Benchmark.ips do |x|<br>  x.report(&quot;option_hash&quot;) { option_hash(foo: 1, bar: 2) }<br>  x.report(&quot;double_splat&quot;) { double_splat(foo: 1, bar: 2) }<br>end</pre><p>… and result (bigger number is better):</p><pre>Warming up --------------------------------------<br>         option_hash   123.232k i/100ms<br>        double_splat    61.146k i/100ms<br>Calculating -------------------------------------<br>         option_hash      1.798M (± 3.6%) i/s -      8.996M in   5.011461s<br>        double_splat    708.545k (± 5.4%) i/s -      3.546M in   5.020927s</pre><p>What’s the optimal usage of double splat?</p><p>You should use double splat in the method definition only if that method defines optional arguments.</p><pre># This is a valid use case since otherwise Ruby will set a hash<br># in argument name `optional`.<br>def valid_use_of_double_splat(optional = nil, **options)</pre><pre># Both of these use case can just use `options = {}` <br>def perform_delivery(recipient:, **options)<br>def associate_user(user, **options)</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee104fbb9cb2" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/think-twice-before-using-double-splat-in-ruby-ee104fbb9cb2">Think twice before using double splat in Ruby</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A new helper to freeze time in Rails 5.2]]></title>
            <link>https://sikac.hu/a-new-helper-to-freeze-time-in-rails-5-2-c99c5b86a008?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/c99c5b86a008</guid>
            <category><![CDATA[activesupport]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Fri, 08 Jun 2018 14:22:18 GMT</pubDate>
            <atom:updated>2018-06-08T14:22:17.935Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>This one is a simple quality-of-life improvement.</p><p><a href="https://github.com/rails/rails/pull/29681">Added time helper method `freeze_time` which is an alias for `travel_to Time.now` by prathamesh-sonpatki · Pull Request #29681 · rails/rails</a></p><p>So, in Rails 5.2, instead of you doing:</p><pre>travel_to(Time.current) do<br>  # ...<br>end</pre><p>You can actually use this new freeze_time helper:</p><pre>freeze_time do<br>  # ...<br>end</pre><p>Internally, both of these methods are just mocking Time.current or Time.now to return given time, essentially “freezing the time”. I’m never a fan of travel_to(current_time) as I feel that it reads weird. I’m going to just use freeze_time from now on if the situation allows.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c99c5b86a008" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/a-new-helper-to-freeze-time-in-rails-5-2-c99c5b86a008">A new helper to freeze time in Rails 5.2</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Expanded rails routes in Rails 6.0]]></title>
            <link>https://sikac.hu/expanded-rails-routes-in-rails-6-0-128bc94134e0?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/128bc94134e0</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[rails-6]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Thu, 07 Jun 2018 14:01:51 GMT</pubDate>
            <atom:updated>2018-06-07T14:01:58.770Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>In Rails 6.0, rails routes command will support --expanded (or -x, the shorthand version). This is similar to \x mode in psql where it will print the record in detailed list instead of in a table.</p><pre>$ rails routes --expanded<br>--[ Route 1 ]-------------------------------------------------------<br>Prefix            | high_scores<br>Verb              | GET<br>URI               | /high_scores(.:format)<br>Controller#Action | high_scores#index<br>--[ Route 2 ]-------------------------------------------------------<br>Prefix            | new_high_score<br>Verb              | GET<br>URI               | /high_scores/new(.:format)<br>Controller#Action | high_scores#new<br>--[ Route 3 ]-------------------------------------------------------<br>Prefix            | blog<br>Verb              |<br>URI               | /blog<br>Controller#Action | Blog::Engine</pre><pre>[ Routes for Blog::Engine ]<br>--[ Route 1 ]-------------------------------------------------------<br>Prefix            | cart<br>Verb              | GET<br>URI               | /cart(.:format)<br>Controller#Action | cart#show</pre><p>I think this is very useful for when you have a lot of complex routes and it’s hard to see unless you have a very huge display.</p><p>You can find more details in the pull request from <a href="https://medium.com/u/37ed4e851db3">Benoit Tigeot</a> here:</p><p><a href="https://github.com/rails/rails/pull/32130">Add &quot;rails routes --expanded&quot; mode by benoittgt · Pull Request #32130 · rails/rails</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=128bc94134e0" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/expanded-rails-routes-in-rails-6-0-128bc94134e0">Expanded rails routes in Rails 6.0</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Relation.pick in Rails 6.0]]></title>
            <link>https://sikac.hu/relation-pick-in-rails-6-0-87b2112f6097?source=rss----457e61dd5fd---4</link>
            <guid isPermaLink="false">https://medium.com/p/87b2112f6097</guid>
            <category><![CDATA[ruby-on-rails]]></category>
            <category><![CDATA[rails]]></category>
            <category><![CDATA[pluck]]></category>
            <category><![CDATA[activerecord]]></category>
            <dc:creator><![CDATA[Prem Sichanugrist]]></dc:creator>
            <pubDate>Wed, 06 Jun 2018 14:01:01 GMT</pubDate>
            <atom:updated>2018-06-06T14:01:01.693Z</atom:updated>
            <cc:license>https://creativecommons.org/licenses/by-nc/4.0/</cc:license>
            <content:encoded><![CDATA[<p>Back in 2012, Rails 3.2.0 added a .pluck method which you can call on a ActiveRecord::Relation or a model class:</p><pre>&gt; User.pluck(:id)<br>#=&gt; [1, 2, 3]</pre><p>But what if you need just the first value from the result? You will probably have to use .limit and #first:</p><pre>&gt; User.limit(1).pluck(:id).first<br>#=&gt; 1</pre><p>Now, starting from Rails 6.0, you will be able to use .pick to pick out a single value. It also accepts multiple values, so you can get value from multiple columns at the same time:</p><pre>&gt; User.pick(:id)<br>#=&gt; 1<br>&gt; User.pick(:id, :username)<br>#=&gt; [1, &quot;sikachu&quot;]</pre><p>However, you should note that this code is just a shorthand for limit(1).pluck(*column_names).first. It does not guaranteed the first value ordered by any column, so you may need to do .order(:id).pick(___) if you really want the first row of your result set.</p><p>You can look at the actual patch in this PR from <a href="https://medium.com/u/54bcbf647830">DHH</a>:</p><p><a href="https://github.com/rails/rails/pull/31941">Add Relation#pick as short-hand for single-value plucks by dhh · Pull Request #31941 · rails/rails</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=87b2112f6097" width="1" height="1" alt=""><hr><p><a href="https://sikac.hu/relation-pick-in-rails-6-0-87b2112f6097">Relation.pick in Rails 6.0</a> was originally published in <a href="https://sikac.hu">Sikachu&#39;s Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>