Get more out of Queries in Rails using Bullet in just 3 steps

3 easy steps to optimize Queries in Rails using Bullet

Bullet is an exciting plugin developed by Richard Huang for Ruby on Rails. It reduces the number of queries made by an application. It was first used in 2009 and since then, it has proved to be a powerful gem that helps monitor Ruby on Rails applications for performance improvements. Bullet seeks several ways to notify problems including JavaScript alerts, XMPP alerts and Growl notifications. Further, it saves its bullet.log statement to trace the exact root of the alert.

Here are some interesting tips to optimize Queries in rails using Bullet.

The traditional method (sans the optimization)

This is the old fashioned method of optimization which was initially used by bullet. This example shows two models; Order and Product, wherein an Order consists of many Products. The code script should be:

In app/controllers/orders_controller.rb

class OrdersController < ApplicationController
def index
@orders = Order.all
end
end

 

In app/views/orders/index.html.erb

<h1>Orders</h1>

<% @orders.each do |order| %>
  <div>
    <h2><%=link_to order.title, order_path(order)%></h2>
  </div>
  <%order.products.each do |product|%>
     <ul class=”product”>
        <li><%=link_to product.title, product_path(product)%></li>
     </ul>
  <%end%>
<% end %>
Source - http://blog.andolasoft.com/2013/05/3-easy-steps-to-optimize-queries-in-rails-using-bullet.html#

This script would however generate some N+1 query issues, the reason being that we have set the query just once in order to get the orders and then separate each query to fetch the products. These are also the kinds of problems that are quite frequently and easily overlooked by programmers. Also, this is where the “Bullet” gem helps in avoiding the issues.

The gem “Bullet” can be integrated to the query in just three easy steps.

Step 1:

Add “Bullet” get to the Gemfile

/Gemfile.rb

gem 'bullet', '4.6.0', :group => “development”

Run the “bundle install” to install the gem “bullet” in development group

Step 2:

Optimize the configuration setting in the development.rb file

For slowing the “bullet” to change its configuration using the after_initialize block contained in the development.rb file. Set the alert as “true” to get the alarms via the browser.

config.after_initialize do
    Bullet.enable = true
    Bullet.alert = true
    Bullet.bullet_logger = true
    Bullet.console = true
    Bullet.rails_logger = true
  end

Step 3:

Restarting the server

You need to both restart the server and reload the page. After step 2, you would see a JavaScript alert popup in the browser with the detected N+1 query. The alert would contain the file that holds the issue and the suggestions to what could be done to override the problem.

The N+1 query can be simultaneously fixed using the following steps:

In Controller,

lass OrdersController < ApplicationController
  def index
    @orders = Order.includes(:products)
  end
end

After you have changed the statement from “Order all” to “Order.includes’ (:products), you need to call eager loading to fetch the products. The date herein would be fetched using two queries, one to obtain the orders and the other to retrieve the products in the orders.

The gem “bullet” can also point out when we are unnecessarily eager loading.

Benefits of optimization

  1. No need to search each line of code to figure out inefficiency in database query
  2. Automatic notification / alert messages
  3. Prevent inefficient database query like N+1
  4. Detect unused eager loading

What Explains the Massive Popularity of Ruby on Rails Among Developers?

Ruby-on-rails

As a language, Ruby on Rails has evolved substantially since its release some 20 years ago. If we talk about the 10 most popular programming languages of all time then Ruby will definitely make into the list. However, this programming language became more popular with the incorporation of Rails; a software library that tremendously expands the usefulness of Ruby programming language. If you are a first time developer then you would certainly be intrigued by this language and would want to check out why is it worth the time and effort to take up this language as your primary development language. For this precise reason, we have put together a list of some compelling benefits that explains the popularity of Ruby on Rails.

The Advantages Offered by Ruby on Rails

When David Heinemeier Hansson created Rails then he preferred to give “Ruby on Rails” as the name to the whole package. The software library gelled up with Ruby so well that the framework is now widely used for building websites. Some of the benefits of using this framework are:

  • The key advantage of Ruby on Rails is that it offers loads of freedom and flexibility. With just few lines of codes, this framework allows to do a lot more work when compared to that of other programming languages, such as Java and .NET. Some programmers may argue that using so much of liberty when writing codes can mess things up. But, they need to understand the fact that till now thousands of websites have been created using this platform, and all are working just fine.
  • When it comes to versatility, Ruby on Rails surpasses most of the conventional programming languages. However, the tools of this framework are so sharp that programmers need to be a little cautious when using it. But again, you have to remain cautious when writing codes, regardless of which programming language you are working with. This is the reason why a lot of web developers prefer Ruby on Rails for its uncluttered and terse syntax which does not require too much punctuation.
  • A lot of developers who work with Rails in the present time feel that this framework has provided them some respite from the worries of handling the hassles related to Java and .NET. In a situation where rapid website development is required, Ruby on Rails provides an efficient and easy to work on platform. Since Java and .NET framework is used mostly in larger organizations that usually make use of conventional software solution, therefore independent web developers are found to be more inclined towards Ruby on Rails.
  • RubyGems is another major reason behind the popularity of Ruby. It is a package manager which makes it very simple to develop and distribute software libraries or gems. You can easily share the gems by uploading them to the website of RubyGems, and from there anyone can get access to the libraries.

GoodWorkLabs too believes that the innovative features of Ruby on Rails help developers to build in useful functionalities in quick time. This is the reason why we have emerged as a specialist in developing world-class software products in Ruby on Rails framework. Contact us for a FREE quote for your requirements.

Garden City RubyConf 2014 – Sponsored by GoodWorkLabs

GoodWorkLabs goodies at RubyConf 2014
GoodWorkLabs brochures
IMG_0501IMG_0500IMG_0496
IMG_0495IMG_0494IMG_0491IMG_0489Vishwas Mudagal, CEO, GoodWorkLabsIMG_0483

Garden City RubyConf that was held at Hotel Atria, Bangalore, was a major success with a packed house. Ruby developers and enthusiasts came in from around the globe.

GoodWorkLabs was a sponsor of the conference.

Check out some of the pics taken during the event, along with GoodWork team.

Via Flickr:
GoodWorkLabs was a proud sponsor of Garden City Ruby Conf 2014 held in Bangalore. Check out some pics.

GoodWorkLabs sponsors Garden City RubyConf 2014

GoodWorkLabs sponsors Garden City RubyConf 2014

 

GoodWorkLabs, the leading Ruby on Rails development firm, is proud to be a sponsor of the 2014 Garden City RubyConf. The first edition of the Ruby Conference is organized by the Ruby Community of Bangalore. It will be held in Hotel Atria, Bangalore, on 3-4 January 2014. We are sure it will be a grand success and set a tone for more versions of Ruby events in Bangalore and India.

Vishwas Mudagal, CEO of GoodWorkLabs, has been a strong supporter of Ruby and Open Source communities in India. In his earlier stints, he had sponsored RubyConf India in 2010 and 2011, supported Global Rails Hackfests and was instrumental in pushing Rails development in India. GoodWorkLabs now continues this trend by supporting Ruby Community in India and making Bangalore a favorite global destination for Ruby programmers and Rails development.

The team at GoodWorkLabs helps companies of all sizes design, development and scale cutting-edge Ruby on Rails applications. We look forward to catching up with you during the RubyConf in Bangalore. Contact us now to book a meeting.

 

Ready to start building your next technology project?