What's new in Rails 5?
Rails 5.0 is almost here! The first beta version has been released already, and you can try out some of the shiny new features right away. So what are the new features? Let’s take a quick tour of the new features and major changes.
Websocket support in Rails is the most talked about feature in Rails 5. This will make it super easy to add realtime features to your app. If you’re planning to explore ActionCable, here’s a couple of getting started guides:
A new attributes API has been introduced that allows you to define
custom attributes on a model or override existing attributes.
For instance, you can create a
MoneyType attribute type,
and define how this gets stored as a
price_in_cents field in the database.
It also allows you to override types of existing database attributes (eg. you were using a decimal field in the database, and now only need the values as integers), or even to define non-persisted attributes on a model. Read more about the attributes API here:
- Attributes API documentation
- Introduction to Rails 5 Attributes
- Using the Rails 5 Attributes API Today, in Rails 4.2
And speaking of ActiveRecord, you might have noticed
Product model above inherits from
This is because Rails now generates that class
so that we can add all our customizations there
instead of mokeypatching
Read more about it here:
After all this time, we finally have the
#or method in ActiveRecord.
We can call
#or on a relation and pass another relation as its argument
and it will generate an OR query.
We can use it like this:
#in_batches method yields a relation,
#find_in_batches which yields an array.
We can use this method for things like this:
If you need a random token in your models,
Rails 5 has introduced
The rails-api gem has been merged into Rails.
It allows you to generate API-only Rails app
and strips out all the middleware that you don’t need
in an app that generates JSON responsed instead of HTML.
You can now generate an API-only app using the
View rendering has been moved out into
and is available for use anywhere via
This lets you render partials into strings and use them
in background jobs and other classes.
Here’s an example of how to render a partial using the render method:
If you’re wondering why you would want to render views outside controllers you can see this in action in the code for the Getting Started with ActionCable guide, where it is used for rendering HTML into a string in background job which is then sent to the clients via websockets.
Update (19 Jan, 2016): From the looks of it, Turbolinks 3 will not ship with Rails 5. Instead, a complete rewrite, Turbolinks 5, will be included when the final version of Rails 5 ships. (Follow the discussion here.) I will update this section when more information is available about TL5.
Turbolinks 3 introduces partial replacement,
which allows you to replace only specific divs on a page.
data-turbolinks-permanent, you can also transfer DOM elements
from page to page along with the states.
This is useful for sections like navbars that do not change across pages.
On the server side, you can trigger partial replacement
using additional options with
For example, take a look at the following code:
If the comment is saved successfully in the above case,
only the div with id “comments” is replaced
with the matching element from the rendered HTML at
In case of an error, only the ‘new_comment’ element, which is the comment form,
is replaced by the matching element from the response.
New command router
Ever accidentally typed
rails db:migrate or
rake generate model?
Figuring out which command to use is often confusing to beginners.
Now all such commands are routed through
so we will be able to use commands like
The rake commands are still available if you want to use them.
Snappier development mode
In development mode, Rails reloads constants when it finds that the file has changed. Until now it was done by checking the mtimes for all the files on each request and reloading if something has changed. Rails 5 introduces an evented file system monitor which asynchronously calls Rails when something changes. As a result, it no longer needs to check all the files on each request, and this makes the development mode much faster. Read more about it here:
The test runned for minitest has seen many improvements, including improved error messages, color output and the ability to run a single test by referring to the line. This has finally brought the minitest Read more about the improvements in the test runner here:
Supports only Ruby 2.2.2+
Rails has always pushed the community towards the latest Ruby versions, and 5.0 is no different. This version will only work on Ruby 2.2.2 and above. This lets Rails to improve performance by making use of the latest improvements in the Ruby garbage collector (like incremental GC and symbol GC).
If you’re upgrading Ruby anyway, you might want to look at Ruby 2.3 which was released recently with a bunch of new features.
With the release of Rails 5, all versions of Rails from 4.1.x and below will no longer be supported. Bug fixes will only be released for Rails 5.0.x and 4.2.x. Now would be a good time to upgrade, at least to 4.2.x if you haven’t already done so. If you’re planning to upgrade to Rails 5.0, these resources could be useful:
- Upgrading to Ruby on Rails 5.0 from Rails 4.2 – application use case
- A Guide for Upgrading Ruby on Rails
The new features in Rails have been discussed in a few other places:
A lot of new features have been introduced in Rails 5, and I might have missed a few interesting ones. Please do leave a comment pointing out any interesting features I might have missed and I’ll add it to the list.