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.
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:
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.
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:
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.
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.
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.
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.
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.