Rails is a web application framework written with ruby and rails 5, the best comprehensive version. In real time, rails 5’s new ActionCable and Websocket feature works with Redis.
It requires Ruby 2.2 or above and with Ruby 2.2 you can get few niceties like symbol garbage collection.
Features:
Let’s have a look at the features:
Action Cable
Action Cable is a framework, used to extend Rails via Websockets to add real-time message passing functionality. It smoothly integrates WebSockets with the rest of application.
It’s very easy to use and designing of chat, notifications, and presence are much easier. Action cable makes easy to add real time features to app which increases the needs for smooth integration.
Turbolinks 5
Turbolinks is the Rails framework for speeding up page navigation by using a bit of JavaScript to request the next page in the background, then dynamically replace the <body> with the new page content. In addition to the normal web functionality of Turbolinks, with version 5 it brings support for mobile clients, allowing native mobile apps to interact.
Use rails instead of rake
For beginners figure out when to use rake and when to use rails is a source of confusion. Now you can run all rake tasks with the rails keyword. For instance,
[code language=”php”]
rake db:migrate[/code]
will now become
[code language=”php”]rails db:migrate[/code]
ActiveRecord Improvements
[code language=”php”]ActiveRecord::Base#where[/code]
.or
In last version of Rails – ActiveRecord::Base#where.not was introduced. With Rails 5 – ActiveRecord::Base#where.or is introduced.
[code language=”php”]
Post.where(‘id = 1’)[/code]
.or
[code language=”php”](Post.where(‘id = 2’))
[/code]
This will create a query as follows –
[code language=”php”]
# => SELECT * FROM posts WHERE (id = 1)[/code]
OR
[code language=”php”](id = 2)
[/code]
#belongs_to is required by default
Rails 5 will have a new configuration option
[code language=”php”] config.active_record.belongs_to_required_by_default = true[/code]
, which triggers 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:
[code language=”php”]
class Book > ActiveRecord::Base
belongs_to :author, optional: true
End
[/code]
has_secure_token landed in ActiveRecord
ActiveRecord model now has token attributes in an easier way. Common scenarios for token attributes are for cases when we need to create an invitation token or a password reset token.
Here is an example:
[code language=”php”]
class Invite < ActiveRecord::Base
has_secure_token :invitation_code
end
invite = Invite.new
invite.save
invite.invitation_code # => 44539a6a59835a4ee9d7b112
invite.regenerate_invitation_code # => true
[/code]
ApplicationRecord
Up to Rails 4.2, all models inherited from ActiveRecord::Base. But from Rails 5, all models do inherit from ApplicationRecord.
[code language=”php”]
class Post < ApplicationRecord
end
[/code]
[code language=”php”]
# app/models/application_record.rb
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
end
[/code]
Render a template outside controllers
Rails 5 allows to render templates or inline code outside controllers. This feature is important and useful for ActiveJob and the new ActionCable.
[code language=”php”]
# render inline code
ApplicationController.render inline: ‘<%= "Hello Rails" %>’ # => "Hello Rails"
# render a template
ApplicationController.render ‘sample/index’ # => Rendered sample/index.html.erb within layouts/application (0.0ms)
# render an action
SampleController.render :index # =>; Rendered sample/index.html.erb within layouts/application (0.0ms)
# render a file
ApplicationController.render file: ::Rails.root.join(‘app’, ‘views’, ‘sample’, ‘index.html.erb’) # => Rendered sample/index.html.erb within layouts/application (0.8ms)
[/code]
Rails API
Rails 5 allows generating API in Rails app, cleans out all the unnecessary middleware for the app. When create a new rails application using new rails API, will get the configuration which assumes you are working with JSON not with HTML.
Command to Create Rails API Application:
[code language=”php”]
rails new myapp-api –api
[/code]
As per rubyonrails.org maintenance policy, the release of Rails 5.0 means that bug fixes only applies to 5.0.x, regular security issues to 5.0.x and 4.2.x, and severe security issues also to 5.0.x and 4.2.x (but when 5.1 drops, to 5.1.x, 5.0.x, and 4.2.x). These mean 4.1.x and below are essentially unsupported! Ruby 2.2.2+ is now the only supported version of Rails 5.0+.
Conclusion
Lots of new features have been added in Rails 5 and it’s time to upgrade your applications for better performance. Hope you liked this topic on Rails 5; please feel free to add up if missing anything.
Thinking to upgrade your app? Let’s have a discussion.