Have you ever opened a closet only to have all the inside contents fall all over you? In the programming setting, this is called as “spaghetti code”. These are the kind of codes that are so jumbled that as soon as you make a little change, everything starts falling apart. As much as it is important to write good codes, it is equally important to keep them clean. But when you are developing a large scale application, it becomes difficult to keep your codes clean and organized. In this article you will read to get more out of queries in rails using bullet in just 3 steps
For a large scale projects with added complexity, manual code refactoring becomes a huge risk and time consuming. Therefore, it is recommended to use third party tools like Bullet Gem for the maintenance, security and optimization of codes.
Bullet plugin, developed by Richard Huang for Ruby on Rails, 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 Gem helps in reducing the number of queries that an application makes. It also helps in finding N+1 queries in the application during development mode by showing the notifications in browser.
N+1 query problem is a situation when the developer makes extra calls to database when s/he wants to get a specific associated data over and over again.
For example,
2.2.1 :005 > Message.all.each {|message| message.conversation.sender_id}This example gets all messages, then iterates each of those records and tries to get messages conversation from database. Solution for this problem is Bullet plugin, which helps to monitor the queries of the application. It notifies any N+1 scenarios and the unused eager loads. Other common solution for this is to use eager loading methods are preload, includes, eager_load. Examples for the following methods:-
2.2.1 :007 > Message.includes(:conversation).references(:conversations).each { |message| message.conversation.sender_id } 2.2.1 :008 > Message.eager_load(:conversation).each { |message| message.conversation.sender_id } 2.2.1 :009 > Message.preload(:conversation).all.each { |message| message.conversation.sender_id } Eager loading is the solution to the N+1 query problem which makes sure we don’t end up running unnecessary queries while looping through an object.
Usage & Configuration Of Bullet Gem :
Below example shows two models; Message and Conversation, wherein a Message consists of many Conversations. Bit of code should be:-In app/controllers/messages_controller.rb class MessagesController < ApplicationController def index @messages = Message.all end end In app/views/messages/index.html.erb <h1>Messages</h1> <% @messages.each do |message| %> <%message.conversations.each do |conversation|%> <ul class=”message_link”> <li><%=link_to conversation.sender_id, conversation_path(conversation)%></li> </ul> <%end%> <% end %>Basically Bullet gem helps to resolving n+1 problem in rails application.
Get More Out of Queries in Rails Using Bullet in Just 3 Steps
Step 1:- Add Bullet gem to gemfile and run bundle install,# For code optimisation gem ‘bullet’, group: [:development, :test]The gem should only be used in the development environment, as you wouldn’t want the users of the application getting alerts about the N+1 query problems. Step 2:- Bullet should be enabled in the application. Just adding the bullet gem won’t notify the bad queries. Configuration is done in the config/environments/development.rb.
config.after_initialize do Bullet.enable = true Bullet.alert = true Bullet.bullet_logger = true Bullet.console = true Bullet.rails_logger = true Bullet.unused_eager_loading_enable = false end Bullet.enable: enable Bullet gem, otherwise do nothing Bullet.alert: pop up a JavaScript alert in the browser Bullet.bullet_logger: log to the Bullet log file (Rails.root/log/bullet.log) Bullet.rails_logger: add warnings directly to the Rails logStep 3: Restart the server. After step 2, we 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. In Controller,
class MessagesController < ApplicationController def index @messages = Message.includes(:conversations) end endRefrences:- GITHUB LINK: – https://github.com/flyerhzm/bullet Our developers are well versed in doing all kinds of ruby on rails development work and handling any kind of project. Contact us to know more about our development skills and the projects which we can handle.
Subscribe For Latest Updates
Related Posts