Difference between Rails 4 and Rails 5

pdkproitf viết ngày 21/06/2017

This article will discuss about the difference between Rails 4 and Rails 5, what new in Rails 5.
Before come to the details let take a quick look at Rails 4 if you have yet know about that - (http://guides.rubyonrails.org/4_2_release_notes.html).
what is changing on new version.

1. Support ruby 2.2.1 or newer

Rails 4 requires Ruby 1.9.3 or higher and prefers Ruby 2.0 while Rails 5 only work with Ruby 2.2.1 or better.
Rails 5 is a new major version of Ruby On Rails, this implicates that the Rails API and requirements can change to deprecate, add new, and/or improve existing APIs, but also to take advantage of important changes in the Ruby language as well.
Symbols were not getting garbage collected prior to Ruby 2.2.1. From the release of Ruby 2.2.1, symbols are garbage collected as well.
Advantages of Symbol GC available in Ruby
Ruby Garbage Collection Example:
alt text
The above example shows that symbols are not garbage collected in ruby 2.1.2
alt text
And this output clearly shows how symbols get garbage collected in Ruby 2.2.1+

When we invoke GC it may/may not start garbage collection - but it will clean symbols that have no reference as shown above.

When symbols were allocated - total size of symbols = 12872
After we invoked Garbage Collector - total size of symbols = 3316
Thus, Garbage collection is very handy feature of Ruby 2.2.1+ which will be explored by Rails 5+ applications.

2. API Deprecation and Cleanup

Rails 5 have the changes related to removing code that was marked as deprecated in previous versions of Rails.
Notable removed APIs are:

  • deliver and deliver!methods have been removed. Instead that you can use deliver_now or deliver_later.
  • *_path helper in email views.

Support for protected_attributes gem
Support for activerecord-deprecated_finders gem.

ActionPack assertions

assert_template and assigns() assertions are deprecated and moved into its own gem rails-controller-testing.

Clean up in Rails 5 also includes dead code and unnecessary tests. Also, mocha is being removed from Rails tests in favor of plain Minitest stubbing.

3. Performance improvements.

With Ruby 2.2.2, Rails 5 should get an improvement in performance, less memory usage, and less time spent in GC. This alone should help alone to bring this performance boost.
Outside, Richard Schneeman had merged a lot of performance improvement to Rails. The patch has improved the performance almost 11%.

More inform, Richard Schneeman is a Ruby developer at Heroku and loves contributing to Rails - https://github.com/schneems.

Avoid Calling to_s on nil

When we call to_s on nil object then - it allocates empty string object and returns. Thus, this is a new string object allocation. If you’re doing this in loop or calling it multiple times somehow then it will be creating a lot of string objects causing increase in memory allocation.

object.to_s if !object.nil?

Basically performing to_s operation only if object is not a nil object.

4. Goodbye, "rake"!**

If you've used Rails before, you'll have to unlearn using the rake command in the terminal to run tasks . In Rails 5, you can do everything with rails command.

Example, rake db:migrate is now rails db:migrate or rake db:restart is now rails restart.

5. ActiveRecord Improvements

or method in ActiveRecord::Relation

Finally ActiveRecord::Relation is getting #or method, this will allow us to write queries with ActiveRecord DSL as follows:

Book.where('id = 1').or(Book.where('status = 3'))
=> SELECT * FROM books WHERE (id = 1) OR (status = 3)

or method accepts a second relation as a parameter that is combined with an or. #or can also accept a relation in a form of model scope.

class Book < ActiveRecord::Base
   scope :new_coming, -> { where(status: 3) }
Book.where('status = 1').or(Book.new_coming)
=> SELECT * FROM books WHERE (status = 1) OR (status = 3)
belongs_to is required by default

From now on every Rails application will have a new configuration option config.active_record.belongs_to_required_by_default = true, it will trigger a validation error when trying to save a model where belongs_to associations are not present.

config.active_record.belongs_to_required_by_default can be changed to false and with this keep old Rails behavior or we can disable this validation on each belongs_to definition, just passing an additional option optional: true as follows:

class Book < ActiveRecord::Base
 belongs_to :author, optional: true

6. Rails 5: Rails API only application

Many a times - people implement Backend in Rails just as a API server - Rails API only application is designed for such use cases.

E.g. If you’re implementing a Mobile Application where you don’t need Web UI implementation - Then you can implement Backend part in Rails and expose APIs which would be called from Mobile Application (either Android / iOS or any other platform)

So, in such cases you don’t need entire Rails Stack for your Application. To solve this - Rails 5 has implemented API only option - which would create minimalistic Rails application.
Why API app instead of complete Rails App?

When you don’t need entire Rails middleware stack. Basically you don’t need ActionController::Base, Asset Pipeline, Views, Helpers etc. - then you should user Rails API app.

Command to create Rails API application:

rails new my_rails_api_app --api
Convert Existing Rails Application to API application

Open your config/application.rb file and add following line -

config.api_only = true
Edit your app/controllers/application_controller.rb

instead of,

class ApplicationController < ActionController::Base

do this: inherit from ActionController::API

class ApplicationController < ActionController::API

7. Rails 5: ActionCable

ActionCable is the new thing in Rails 5, it is a framework for real time communication over web sockets.
ActionCable is Framework for Real-Time communication over WebSockets. ActionCable is integrated websocket for Rails Applications. ActionCable is being implemented as a gem at Rails/ActionCable. This would be Merged with Rails once implementation is completed and stable.

What problem is solved by ActionCable?

Suppose you have a mailing application, where you would want to update a new email in the browser as soon as received.

Possible Solution

You can keep polling after some time interval to server side (backend) and if you get new email in response then update the DOM with the content received from backend via javascript. But, this would result in many number of polls void/useless.

WebSocket is a protocol providing full-duplex communication channels over a single TCP connection. [Source]

ActionCable Terminologies:

Cable - Cable is just like a way to communicate between browser (client) and server through a connection which is made using websockets

Connection - Connection is created using WebSockets

Channel - A Cable can have multiple channels. Channels need to be created to send or receive for various functionalities that need solution to the similar problem discussed above.

Broadcast - Server can broadcast to different channels particular data


What make power on Rails 5.

  • So far, In the version 5, Raisl changed to support Ruby 2.2.1 or newer.
  • Rails 5 also changed a little bit on commands and merged Richard Schneeman commit to make performance improvements 11%,.
  • Not just like that, Rails 5 decided support more to implement Backend in Rails just as a API server when release Rails API only application.
  • The last but not the least, Rails 5 is integrated websocket for Rails Applications.

Rails 4 was great choice for developer. But with all of changes on Rails 5, it really excites me and I think you too. Beside, Rails 5 quite new So You should concern that there may be many gems that are not ready for 5.

Thank you :smile: !

Bình luận

{{ comment.user.name }}
Bỏ hay Hay
Male avatar
{{ comment_error }}

Hiển thị thử

Chỉnh sửa



3 bài viết.
0 người follow
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
2 0
Welcome back, Before reading this chapter, let make sure that you already read through on chapter (Link). In previous chapter I already talk about ...
pdkproitf viết 8 tháng trước
2 0
1 0
This article will discuss about the basic styles are used common on Rails include: How to use Ruby convention on Rails and Rails convention. Why c...
pdkproitf viết 8 tháng trước
1 0


{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}

{{userFollowed ? 'Following' : 'Follow'}}
3 bài viết.
0 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!