It’s been two years since the last Rails release. During these two years, a lot of new stuff bumped into web world. New JavaScript frameworks (they just keep coming!), Ruby frameworks, and new ‘rails-way hate camps’.

The fact is, Rails 5 doesn’t mind the atmosphere around it. It’s still a framework that encourages you to create software with specific structure and conventions. Upcoming changes prove that as well.

Small changes

There are lot of small changes, but I will list just a few of them:

1. No more rake

It’s quite simple to explain this one. In order to maintain consistency, all existing commands that begin with ‘rake’ will be substituted with ‘rails’ commands. See examples below:

rake db:migrate -> rails db:migrate
rake db:setup -> rails db:setup
rake test -> rails test

2. Be aware of empty foreign_keys!

When you setup `belongs_to` in your model, Rails 5 will automatically validate its relation model presence on objects. If you don’t want to use this feature, you need to pass `optional: true` option.

Please be aware when you update Rails 4 to Rails 5, guys!

3. Change attribute type of existing model

This one is tricky, and I presume it could do a lot of damage with improper use.

Let’s say you have a model with a float type field. In Rails 5, you will be able to change this attribute type by typing in the model’s body:

attribute :your_field, :integer

So, in the database you will have float type, but when you request the data within active record object, you will get an integer field.

4. Secret token

If you create an Invitation model, you’ll often need to have some kind of object token. Good news is, that there will be a method for token generation. You just need to include this in your model:

has_secure_token: :your_token_name

When you save your object, the token will be generated automatically. So basically, instead of writing the `before_save` stuff, you can use this helper method.

5. Minitest upgrade

In all my projects I’m using Rspec, not minitest. But, as far I can see, minitest will get result printing similar to Rspec, and will offer you some new options, like running tests filtered by a string, or executing a specific number of the test.

6. Where.or

Long story short, the code speaks for itself, and this will be possible:

User.where(name: “John").or(User.where(name: “Jane”)) # => SELECT * FROM users WHERE (name = John) OR (name = Jane)

7. Render view outside the controller

It’s rarely used, and I never needed it, but you can use a new method for this if you want:

ApplicationController.render 'path/to/view'

8. Rails api gem

You can start a new rails project and tell the generator that you would need only api support. So you don’t need views, maybe sprockets and stuff like that. You can do it by adding the `--api` option.

rails new some_app_name --api

There are a lot of small features that ship with new Rails, but listing them with description in this post would be too cumbersome, especially that there is no official site with release notes yet. But, let’s say that features listed above are just tiny upgrades/changes – you’ll find the real deal below.

Turbolinks

This is funny. When turbolinks came in, I remember the first thing we have done in the project was removing them, because they caused more harm than good. There was a trend to use some kind of big javascript player, like Angular or Backbone, and adding turbolinks to the stack could be risky.

Now the dust has settled, and huge amount of projects are just single page applications with lots, lots of javascript and backend code – well, Turbolinks 3.0 seems to be a remedy for this.

The idea of using new turbolinks is very close to React philosophy. Basically, when you need an interactive component, and you want to change it, you would render the whole component and replace the old one with the new one.

All that can be achieved without writing a single line of javascript code, as all the magic happens on the server side. And the word ‘magic’ is the key to that. I’m just a little worried that too much magic can kill you.

I think the direction Turbolinks are heading to is good, but I still can’t imagine using them in production apps (however, it works in Shopify right now). I would still stick with React.

Action Cable

This looks like an upgrade I will use with joy. This is the websockets for Rails. If you want to have some kind of ‘live’ events (let’s forget the action controller live [*]), you would probably use some external solution.

Websockets are the part of Rails app itself now. How do they work? The idea is to create a connection (cable/web socket) to the application. On that cable, you can layer different channels, like chat or notification. Messages would be sent to channels by a broadcaster through the cable. And, it is all handled by redis server. Simple, right?

I can’t wait to test this out. I hope it will work out of box.

Summary

When you look at all these changes, you see that Rails 5 is more like an evolution than a revolution. Among all the new features, only the Action Cable looks interesting. Using turbolinks is tempting, but I’m not yet convinced.
Rails society is getting older, and people start to see that the rails-way isn’t the best option for large projects. Maybe Rails 6 will address this issue? Well, we’ll have to wait two years for that.

Post tags:

Join our awesome team
Check offers

Work
with us

Tell us about your idea
and we will find a way
to make it happen.

Get estimate