Rails 5: What’s new

Cover for Rails 5: What’s new


Share this post on


If you’re interested in translating or adapting this post, please contact us first.

Test runner, where.or, new test request API, Render From Anywhere and no more alias_method_chain.

I’ve found a great post regarding the upcoming changes in Rails 5 by Jeroen van Baarsen, and you should check it out if you’re interested in Rails Edge developments.

However, I felt like this post is just not enough since there are so many interesting things in the upcoming Rails 5 release. So here goes my very own “yet another review of Rails 5 features”.

Rails Test runner

The new runner is, in fact, my favourite piece of Rails 5. I love minitest, and I always was a bit envy of RSpec users, as they have a great runner that allows you to specify the file and line number of a failing spec.

Now, thanks to Yves Senn, we have the same feature when using Rails’ minitest:

$ bin/rails test test/models/post_test.rb
Run options: --seed=48587
# Running:
Finished in 0.024376s, 205.1198 runs/s, 287.1677 assertions/s.
1) Failure:
PostTest#test_failling_one [test/models/post_test.rb:32]:
Failed refutation, no message given
2) Failure:
PostTest#test_has_likes [test/models/post_test.rb:23]:
Failed assertion, no message given.
5 runs, 7 assertions, 2 failures, 0 errors, 0 skips
Failed test:
rails test test/models/post_test.rb:32
rails test test/models/post_test.rb:23

At the bottom of this output, you can see the runner suggesting commands to execute only the failing samples line by line.


Previously, to use OR conditions with the ActiveRecord query builder, we had to construct the query manually by using Arel:


In 2013, Rails 4.0 introduced where.not chain method in ActiveRecord, and now in 5.0 we finally have where.or:

Post.where("id = 1").or(Post.where("id = 2"))
# => SELECT * FROM posts WHERE (id = 1) OR (id = 2)

belongs_to is required by default

DHH wrote that almost every belongs_to declaration seemed to be a required association. Later, @simi implemented it.

However, if your belongs_to association is not required, you could use the optional: true option:

class User
  belongs_to :org, optional: true


Do you use the has_secure_password option introduced back in Rails 3.1?

From now on, for fields like subscription token, we have has_secure_token:

class User < ActiveRecord::Base
  has_secure_token :token1, :token2, key_length: 30

user = User.new
user.token1 # => “973acd04bc627d6a0e31200b74e2236”
user.token2 # => “e2426a93718d1817a43abbaa8508223”
user.regenerate_token1! # => true
user.regenerate_token2! # => true

No more alias_method_chain

alias_method_chain method was there since the very early Rails versions and it was used all over the Rails code base and by various gems.

It was widely used to monkey patch or tweak any previously declared method:

module ActionController
  class Base
    # overriding ActionController::Base load from gem
    alias_method_chain :params, :patched
    def params_with_patched
      result = params_without_patched
      # we can access and modify the result here

But now, with Module#prepend available since Ruby 2.0 we can use native Ruby features to achieve the same result:

module PatchedParams
  def params
    result = super
    # we can access and modify result here

In Rails 5, alias_method_chain will be deprecated in favor of the native Module#prepend.

Read more about alias_method chain here:

Keyword arguments in controller & integration tests

Here is an example snippet you could find in a controller test:

get :index, { id: 1 }, nil, user_id: 1

Does it make a lot of sense what every argument means exactly? Not much, so I proposed the new syntax with keyword arguments support for request methods:

post :create, params: { y: x }, session: { key: 'value' }
get :view, params: { id: 1 }
get :view, params: { id: 1 }, format: :json

These changes apply when using any test framework — it doesn’t matter if you use RSpec or Minitest. The old syntax works as well, but it’s considered deprecated and will be removed later in Rails 5.1.

Render From Anywhere

Earlier we had to use gems like render_anywhere to render any views outside of controller — for example in Rake tasks or background jobs.

In Rails 5 you’ll have the ability to render your views from anywhere:

ApplicationController.render _render_options_

Besides the usual rendering options, in Rails 5 there is a new assigns option for passing instance variables to templates.

Hope you like these new features. We will try to review more of them as we come closer to Rails 5 release candidates.

A great way to stay in touch with Edge Rails development is to subscribe to This week in Rails to receive weekly email with scoop of interesting commits, pull requests and other stuff that is happening with Rails Core.

Solve your problems with 1-1 guidance

Are you fighting with the challenges of improving performance, scaling, product shipping, UI design, or cost-effective deployment? Our experts in developer-first startups can offer free tailored recommendations for you—and our engineering team to implement the bespoke strategy.

Reserve your spot
Launch with Martians

How can we help you?

Martians at a glance
years in business

We transform growth-stage startups into unicorns, build developer tools, and create open source products.

If you prefer email, write to us at surrender@evilmartians.com