<?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: Ronak Bhatt</title>
    <description>The latest articles on DEV Community by Ronak Bhatt (@ronakabhattrz).</description>
    <link>https://dev.to/ronakabhattrz</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%2F461949%2Fec869e84-b80e-4d53-b915-4f6d4e8946d4.jpg</url>
      <title>DEV Community: Ronak Bhatt</title>
      <link>https://dev.to/ronakabhattrz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ronakabhattrz"/>
    <language>en</language>
    <item>
      <title>Rails 6 - Active Storage changes
</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Sat, 07 Aug 2021 18:41:05 +0000</pubDate>
      <link>https://dev.to/ronakabhattrz/rails-6-active-storage-changes-1lkl</link>
      <guid>https://dev.to/ronakabhattrz/rails-6-active-storage-changes-1lkl</guid>
      <description>&lt;p&gt;Active Storage was introduced in Rails 5.2. In Rails 6, there are enhancements done to Active Storage.&lt;/p&gt;

&lt;p&gt;Let’s explore them.&lt;/p&gt;

&lt;h4&gt;
  
  
  &lt;strong&gt;mini_magick replaced by image_processing gem&lt;/strong&gt;
&lt;/h4&gt;

&lt;p&gt;In Rails 5.2, Active Storage was using mini_magick to handle image resizing (variants).&lt;/p&gt;

&lt;p&gt;In Rails 6, a new gem called image_processing is used by default to handle image variants. (commit)&lt;/p&gt;

&lt;p&gt;The image_processing gem has below advantages:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;New methods #resize_to_fit, #resize_to_fill, etc also sharpens the thumbnail after resizing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It fixes the image orientation automatically. This can be referred here.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It provides another backend libvips that has significantly better performance than ImageMagick. With ImageMagick, resizing and sharpening a 1600x900 image to 800x800 is 1.87x slower, and to 300x300 is 1.18x slower. On libvips it doesn’t go above 1.20x slower, on average it’s only about 1.10x slower.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Fix for has_many_attached field in update query
&lt;/h4&gt;

&lt;p&gt;Let’s say we have a &lt;code&gt;User&lt;/code&gt; class and it has field images. Users can upload multiple images to their profiles. So we add &lt;code&gt;has_many_attached&lt;/code&gt; method to User class as shown below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_many_attached :images
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;update query replaces the existing collection instead of appending to the collection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;user.images.attach(filename: "profile_pic.jpg")

user.images.count
=&amp;gt; 1

blog = ActiveStorage::Blob.create_after_upload!(filename: "updated_pic.jpg")
user.update(images: [blog])

user.images.count
=&amp;gt; 1
user.images.first.filename
=&amp;gt; "updated_pic.jpg"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Note:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;We can append files by using the attach function.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;More changes related to Active Storage can be found &lt;a href="https://edgeguides.rubyonrails.org/6_0_release_notes.html#active-storage"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Great Ruby Gems Most People Haven’t Heard About</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Fri, 01 Jan 2021 06:39:34 +0000</pubDate>
      <link>https://dev.to/mainstreet/great-ruby-gems-most-people-haven-t-heard-about-56jh</link>
      <guid>https://dev.to/mainstreet/great-ruby-gems-most-people-haven-t-heard-about-56jh</guid>
      <description>&lt;p&gt;What are the best Ruby gems that you can use in your Rails projects?&lt;/p&gt;

&lt;p&gt;That’s what you’ll discover in this article!&lt;/p&gt;

&lt;p&gt;I’m going to give you 7 gems, but not the same old gems that you’ve seen a million times, I’m going to be sharing with you some gems that are very helpful, but little-known.&lt;/p&gt;

&lt;p&gt;But before we do that…&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;A warning.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I’ve seen developers that pull in a gem for just about everything.&lt;/p&gt;

&lt;p&gt;If it remotely sounds like it could be helpful.&lt;/p&gt;

&lt;p&gt;Without taking a moment to think if that gem solves the problem they have if it’s the best option, well-maintained &amp;amp; documented, etc.&lt;/p&gt;

&lt;p&gt;That’s a mistake.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;1. Find Dead Routes To Keep Your Code Clean&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;As your Rails application grows, you’ll accumulate more &amp;amp; more routes.&lt;/p&gt;

&lt;p&gt;You’ll change code &amp;amp; some of these routes become stale.&lt;/p&gt;

&lt;p&gt;No longer needed…&lt;/p&gt;

&lt;p&gt;But they’ll stay there, in your &lt;code&gt;config/routes.rb&lt;/code&gt;, making it harder to manage.&lt;/p&gt;

&lt;p&gt;How do you find which routes to delete?&lt;/p&gt;

&lt;p&gt;There is a gem called &lt;code&gt;traceroute&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It loads your routes &amp;amp; compares them to the controller actions you’ve defined.&lt;/p&gt;

&lt;p&gt;But right now it has two limitations:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It doesn’t work with Rails 6&lt;/li&gt;
&lt;li&gt;It doesn’t detect implicit controller actions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can find it &lt;a href="https://github.com/amatsuda/traceroute" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;2. Make Your Migrations Safer So You Can Avoid Problems Rails migrations can cause a lot of problems if done wrong.&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Removing a column can cause problems because Rails has a cache of columns, and running a migration doesn’t reset this cache.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Another example:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you add an index &amp;amp; you’re running PostgreSQL the whole table gets locked until the operation is complete.&lt;/p&gt;

&lt;p&gt;Not good for your users.&lt;/p&gt;

&lt;p&gt;Your application won’t be able to respond to requests that need to work with a locked table.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The good news?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You don’t have to remember all of these things.&lt;/p&gt;

&lt;p&gt;With the help of the &lt;code&gt;strong_migrations&lt;/code&gt; gem, you’ll know when you have one of these unsafe migrations before it makes it into production.&lt;/p&gt;

&lt;p&gt;You can find it &lt;a href="https://github.com/ankane/strong_migrations" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;




&lt;h3&gt;
  
  
  &lt;strong&gt;3. Find Your Slow Tests &amp;amp; Make Them Faster&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Slow tests are no fun.&lt;/p&gt;

&lt;p&gt;But there are tools to help you find why your &lt;a href="https://www.rubyguides.com/2018/07/rspec-tutorial/" rel="noopener noreferrer"&gt;tests&lt;/a&gt; are slow so you can fix them!&lt;/p&gt;

&lt;p&gt;One of these tools is the combination of test-prof + ruby-prof.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Here’s how to use it:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;TEST_RUBY_PROF=1 rake
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Outputs:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;%self       total       self         wait        child       calls   name
 43.21      2.001     2.001     0.000     0.000        1     Kernel#sleep
   2.97      0.184     0.138     0.000     0.046     1640   Array#permutation
   1.39      0.064     0.064     0.000     0.000      144   PG::Connection#async_exec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I can clearly see my call to &lt;code&gt;sleep&lt;/code&gt;, but this could easily be an API call, reading a big file, a slow SQL query, etc.&lt;/p&gt;

&lt;p&gt;Another thing you can do is to use the event profiler.&lt;/p&gt;

&lt;p&gt;Btw, &lt;a href="https://github.com/rails/rails/pull/31900" rel="noopener noreferrer"&gt;Rails 6 added parallel testing&lt;/a&gt;, you’ve to enable this in &lt;code&gt;test/test_helpers.rb&lt;/code&gt; if you’re upgrading your current project.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;4. Find Out Which Code Is Used In Production &amp;amp; Which Isn’t&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Let’s finish with another tool to help you improve your code.&lt;/p&gt;

&lt;p&gt;It’s called &lt;code&gt;coverband&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If you run this gem in production (low overhead), you’ll get a coverage report of what code is being run.&lt;/p&gt;

&lt;p&gt;You can even track unused views!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvhvyrv6e10ifbmcv3220.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvhvyrv6e10ifbmcv3220.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This can help you make decisions when deleting code &amp;amp; cleaning your project.&lt;/p&gt;

&lt;p&gt;Don’t let unused code build up!&lt;/p&gt;

&lt;p&gt;You can find it &lt;a href="https://github.com/danmayer/coverband" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Thanks for reading!&lt;br&gt;
I hope that helps someone. Thanks :).&lt;/p&gt;

&lt;p&gt;I’d love to hear thoughts or comments around this. Feel free to email me at &lt;a href="mailto:ronakabhattrz@gmail.com"&gt;ronakabhattrz@gmail.com&lt;/a&gt; or hit me up on Twitter, &lt;a class="mentioned-user" href="https://dev.to/ronakabhattrz"&gt;@ronakabhattrz&lt;/a&gt; .&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>What is the Difference between Ruby Blocks, Procs and Lambdas
</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Fri, 13 Nov 2020 09:20:36 +0000</pubDate>
      <link>https://dev.to/mainstreet/what-is-the-difference-between-ruby-blocks-procs-and-lambdas-oen</link>
      <guid>https://dev.to/mainstreet/what-is-the-difference-between-ruby-blocks-procs-and-lambdas-oen</guid>
      <description>&lt;p&gt;Having a problem understanding the difference between ruby blocks, procs and lamdas. What are the blocks? What is the difference between procs and lambdas? Let's break this down.&lt;/p&gt;

&lt;h4&gt;
  
  
  BLOCKS:
&lt;/h4&gt;

&lt;p&gt;A block is a collection of code enclosed in a do/end statement or between braces { }. They are chunks of code that you can pick up and drop into another method as input or chunk of code that you associate with a method call. If you have used each before to loop through an Enumerable then you have used blocks.&lt;/p&gt;

&lt;h4&gt;
  
  
  Defining a block
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def block_method
  puts "we are in the method"
end

block_method { puts "The block is called"}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  PROCS
&lt;/h4&gt;

&lt;p&gt;So what if you want to pass two blocks to your function. How can you save your block into a variable?&lt;br&gt;
Ruby introduces procs so that we are able to pass blocks around. Proc objects are blocks of code that have been bound to a set of local variables.Once bound, the code may be called in different contexts and still access those variables.&lt;/p&gt;
&lt;h4&gt;
  
  
  Defining procs
&lt;/h4&gt;

&lt;p&gt;You can call new on the Proc class to create a proc . You can use the kernel object proc. Proc method is simply an alias for Proc.new. This can be assigned into a variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;factor = Proc.new {|n| print n*2 }

or 

factor = proc {|n| print n*2}

//using the proc value

[3,2,1].each(&amp;amp;factor)

&amp;gt;642
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  LAMBDAS
&lt;/h4&gt;

&lt;h5&gt;
  
  
  Can be defined using the method lambda or can be defined as stuby lambda
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;lamb = lambda {|n| puts 'I am a lambda' }
lamb = -&amp;gt; (n) { puts 'I am a stuby lambda' }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Difference between Procs and Lambdas
&lt;/h4&gt;

&lt;p&gt;Procs don’t care about the correct number of arguments, while lambdas will raise an exception.&lt;/p&gt;

&lt;p&gt;Return and break behaves differently in procs and lambdas&lt;br&gt;
Next behaves same way in both procs and lambdas&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;p&gt;rubyguides.com : &lt;a href="http://www.rubyguides.com/2016/02/ruby-procs-and-lambdas/"&gt;Click Here!!!!&lt;/a&gt;     &lt;/p&gt;

&lt;p&gt;I hope that helps someone. Thanks :).&lt;/p&gt;

&lt;p&gt;Thanks for reading. 🙂&lt;/p&gt;

&lt;p&gt;I’d love to hear thoughts or comments around this. Feel free to email me at &lt;a href="mailto:ronakabhattrz@gmail.com"&gt;ronakabhattrz@gmail.com&lt;/a&gt; or hit me up on twitter, &lt;a class="mentioned-user" href="https://dev.to/ronakabhattrz"&gt;@ronakabhattrz&lt;/a&gt; .&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails 6, Stimulus  events</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Tue, 27 Oct 2020 12:54:57 +0000</pubDate>
      <link>https://dev.to/mainstreet/rails-6-stimulus-events-3bje</link>
      <guid>https://dev.to/mainstreet/rails-6-stimulus-events-3bje</guid>
      <description>&lt;p&gt;An action is a connection between:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a controller method&lt;/li&gt;
&lt;li&gt;the controller’s element&lt;/li&gt;
&lt;li&gt;a DOM event listener&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;gallery_controller.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// controllers/gallery_controller.js
import { Controller } from "stimulus"

export default class extends Controller {
  next(event) {
    alert("Justclick !!! :")
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  View File Setup
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div data-controller="gallery"&amp;gt;
  &amp;lt;button data-action="click-&amp;gt;gallery#next"&amp;gt;…&amp;lt;/button&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Descriptors
&lt;/h5&gt;

&lt;p&gt;The data-action value click-&amp;gt;gallery#next is called an action descriptor. In this descriptor:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;click is the name of the DOM event to listen for&lt;/li&gt;
&lt;li&gt;gallery is the controller identifier&lt;/li&gt;
&lt;li&gt;next is the name of the method to invoke&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  The full set of these shorthand pairs is as follows:
&lt;/h5&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jqk87rgkbp6v8xv77d1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F9jqk87rgkbp6v8xv77d1.png" alt="Screenshot 2020-10-27 at 6.24.03 PM"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I hope that helps someone. Thanks :).&lt;/p&gt;

&lt;p&gt;Thanks for reading. 🙂&lt;/p&gt;

&lt;p&gt;I’d love to hear thoughts or comments around this. Feel free to email me at &lt;a href="mailto:ronakabhattrz@gmail.com"&gt;ronakabhattrz@gmail.com&lt;/a&gt; or hit me up on twitter, &lt;a class="mentioned-user" href="https://dev.to/ronakabhattrz"&gt;@ronakabhattrz&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>How to Use Scopes in Ruby on Rails</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Thu, 15 Oct 2020 07:24:53 +0000</pubDate>
      <link>https://dev.to/mainstreet/how-to-use-scopes-in-ruby-on-rails-1mhj</link>
      <guid>https://dev.to/mainstreet/how-to-use-scopes-in-ruby-on-rails-1mhj</guid>
      <description>&lt;p&gt;Scopes are custom queries that you define inside your Rails models with the scope method.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Every scope takes two arguments:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A name, which you use to call this scope in your code&lt;br&gt;
A lambda, which implements the query&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It looks like this:&lt;br&gt;
&lt;/p&gt;


&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  scope :active, -&amp;gt; { where("status: 0") }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As a result of calling a scope, you’ll get an ActiveRecord::Relation object.&lt;/p&gt;

&lt;p&gt;Which means you can chain &amp;amp; combine scopes!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;User.active
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  When To Use Scopes?
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;Let’s see an example.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def index
  @books = Book.where("LENGTH(title) &amp;gt; 20")
end 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is an index controller action that wants to display books with titles longer than 20 characters.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s fine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But if you want to use this query in other places, you’re going to have duplicated code.&lt;/p&gt;

&lt;p&gt;Duplicated code makes your project harder to maintain.&lt;/p&gt;

&lt;p&gt;Let’s move this query into a scope.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Like this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Book
  scope :with_long_title, -&amp;gt; { where("LENGTH(title) &amp;gt; 20") }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Now our controller action looks like this:&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def index
  @books = Book.with_long_title
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Summary&lt;br&gt;
&lt;code&gt;Good job! As a result of reading this article, you’ve learned how to use Rails scopes in the most effective way.&lt;/code&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;code&gt;Don’t forget to put this new knowledge into practice so you can remember how it works.&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Thanks for reading. 🙂&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;I’d love to hear thoughts or comments around this. Feel free to email me at &lt;a href="mailto:ronakabhattrz@gmail.com"&gt;ronakabhattrz@gmail.com&lt;/a&gt; or hit me up on twitter, &lt;a class="mentioned-user" href="https://dev.to/ronakabhattrz"&gt;@ronakabhattrz&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Rails 6, Stimulus and Select2</title>
      <dc:creator>Ronak Bhatt</dc:creator>
      <pubDate>Wed, 02 Sep 2020 09:01:24 +0000</pubDate>
      <link>https://dev.to/mainstreet/rails-6-stimulus-and-select2-4901</link>
      <guid>https://dev.to/mainstreet/rails-6-stimulus-and-select2-4901</guid>
      <description>&lt;p&gt;This is just a quicky, to help out anyone using &lt;a href="https://rubyonrails.org"&gt;Rails 6&lt;/a&gt;, or any &lt;a href="https://stimulusjs.org"&gt;Stimulus-enabled&lt;/a&gt; Rails project and &lt;a href="https://select2.org"&gt;Select2&lt;/a&gt;, the amazing jQuery plugin for better select boxes.&lt;/p&gt;

&lt;h5&gt;
  
  
  add select2 &amp;amp; select2-bootstrap-theme package in web-pack
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn add select2
yarn add select2-bootstrap-theme
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Simple Select2 + Stimulus Controller
&lt;/h5&gt;

&lt;p&gt;&lt;strong&gt;select2_controller.js&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { Controller } from "stimulus"
import $ from 'jquery';

require("select2/dist/css/select2")
require("select2-bootstrap-theme/dist/select2-bootstrap")

import Select2 from "select2"

export default class extends Controller {
  connect() {
    $('.content-search').select2();
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h5&gt;
  
  
  Replace f.select dropdown according to select2 specifications
&lt;/h5&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;div class="form-group" data-controller='select2'&amp;gt;
  &amp;lt;%= f.select :user_id, User.all.map { |user| user.name }, {include_blank: false, required: true, include_hidden: false}, class: 'form-control content-search' %&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I hope that helps someone. Thanks :).&lt;/p&gt;

&lt;p&gt;I’d love to hear thoughts or comments around this. Feel free to email me at &lt;a href="mailto:ronakabhattrz@gmail.com"&gt;ronakabhattrz@gmail.com&lt;/a&gt; or hit me up on Twitter, &lt;a class="mentioned-user" href="https://dev.to/ronakabhattrz"&gt;@ronakabhattrz&lt;/a&gt;.&lt;/p&gt;

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