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.
Let’s have a look at the features:
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.
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,
will now become
In last version of Rails – ActiveRecord::Base#where.not was introduced. With Rails 5 – ActiveRecord::Base#where.or is introduced.
Post.where('id = 1')
(Post.where('id = 2'))
This will create a query as follows –
# => SELECT * FROM posts WHERE (id = 1)
(id = 2)
#belongs_to is required by default
Rails 5 will have a new configuration option
config.active_record.belongs_to_required_by_default = true
, 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:
class Book > ActiveRecord::Base belongs_to :author, optional: true End
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:
class Invite < ActiveRecord::Base has_secure_token :invitation_code end invite = Invite.new invite.save invite.invitation_code # => 44539a6a59835a4ee9d7b112 invite.regenerate_invitation_code # => true
Up to Rails 4.2, all models inherited from ActiveRecord::Base. But from Rails 5, all models do inherit from ApplicationRecord.
class Post < ApplicationRecord end
# app/models/application_record.rb class ApplicationRecord < ActiveRecord::Base self.abstract_class = true end
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.
# 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)
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:
rails new myapp-api –api
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+.
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.