All Articles

Tips for Writing Fast Rails: Part 1

Rails opens a new window is a powerful framework. You can write a lot of features in a short period of time. In the process you can easily write code that performs poorly.

At OmbuLabs opens a new window we like to maintain Ruby on Rails applications opens a new window . In the process of maintaining them, adding features and fixing bugs, we like to improve the code and its performance (because we are good boy scouts opens a new window !)

Here are some tips based on our experience.

Prefer where instead of select

When you are performing a lot of calculations, you should load as little as possible into memory. Always prefer a SQL query vs. an object’s method call.

Read more of Tips for Writing Fast Rails: Part 1 opens a new window

Spy vs Double vs Instance Double

When writing tests for services, you may sometimes want to use mock objects instead of real objects. In case you’re using ActiveRecord and real objects, your tests may hit the database and slow down your suite. The latest release of the rspec-mocks opens a new window library bundled with RSpec 3 opens a new window includes at least three different ways to implement a mock object.

Let’s discuss some of the differences between a spy, a double and an instance_double. First, the spy:

[1] pry(main)> require 'rspec/mocks/standalone'
=> true
[2] pry(main)> user_spy = spy(User)
=> #<Double User>
[3] pry(main)> spy.whatever_method
=> #<Double (anonymous)>
Read more of Spy vs Double vs Instance Double opens a new window

Tips for Upgrading from Rails 3.2 to 4.0

There are already quite a few guides in the wild to help with the upgrade of Rails 3.2 to Rails 4.0.

The official Rails guide opens a new window for upgrading from Rails 3.2 to 4.0 is very thorough. With the recent release of Rails 5.0, apps currently in production running Rails 3.2 should probably be updated to any stable Rails 4 release as soon as possible.

There is even an e-book about upgrading from Rails 3 to 4 opens a new window , which serves as a useful guide to make this upgrade easier, and also helps understand the advantages & disadvantages of this new (soon to be old) version.

However, if you’re using any non-standard gems, you’re mostly on your own. Some gems stopped being maintained before Rails 4 was released, as was the case with CanCan opens a new window , a well known authorization library. After many open pull requests were left unmerged opens a new window , CanCanCan opens a new window was released. It is a community driven effort to have a semi-official fork of CanCan. It serves as a drop-in replacement for people who want to use CanCan after upgrading to Rails 4.

Read more of Tips for Upgrading from Rails 3.2 to 4.0 opens a new window