What’s new in Rails 6?

Tram Ho

Rails 6 has been released and it comes packed with features that are beneficial for both small and large applications, it seems that a lot of speed and scalability improvements have been brought in Rails 6.

I have read through the CHANGELOGs part of Rails 6 (ActiveRecord, ActionPack, ActiveSupport, etc.) and picked out a few features that I find most interesting.

Active Record

Add the basic API for connection transformation for multi-database support.

Eileen M. Uchitelle extracted from Github’s codebase and brought this great feature to Rails, a solution that converts the database connection for a particular model or a specific query. The code for this feature is part of the following pull request: https://github.com/rails/rails/pull/34052 , it is doc quite complete and easy to read.

Job slow queries can be read from a database copy using a simple API

The database.yml file will need a little longer

Gannon McGibbon then added a bit of code to support the hash and config url in ActiveRecord::Base.connected_to ‘s database hash and enabled us to write the following code.

Add Relation#pick , pluck for single values

As the title suggests, using pick will be the same as the pluck but used for single values

The columns can be configured implicitly

For apps that don’t use incremental IDs, this is valuable. Now we will be able to set the order of the records with just one command line.

Action Mailbox

Action mailbox has been released in the sixth release of Ruby on Rails, I am sure there will be more articles about this feature in the future. Action Mailbox provides a set of tools that allow applications to better integrate incoming emails.

The basic configuration is in the Action Mailbox Basics Rails Guide , there are a few interesting ideas about this feature that the conversations will be able to happen automatically in both platforms and emails and are interchangeable, for example Like comments on Github from email, help emails turn into tickets …

Note that the Action Mailbox requires Active Job and Active Storage and needs a table in the database for it. Most documentation on classes is also available at ActionMailbox :: Base class documentation , ActionMailbox :: InboundEmail class documentation, and working with parsed emails is done using the Mail gem.

Action Text

Action Text is the Trix editor of Basecamp included in ActiveRecord models. It gives us the has_rich_text method and we can apply it to the model we want and the Action Text will take care of the rest. The Action Text require ActiveStorage and some tables to store its metadata so keep in mind that you will need to switch to using an ActiveStorage provider that is not on disk if your application needs more than one server.

Documentation for this feature is available on Rails Guides and it is pretty straightforward.

Action Pack

ActionDispatch :: HostAuthorization

Host Authorization is a new middleware that helps defend against DNS rebinding attacks (which are the ones that create links to websites containing malicious JS code) by clearly indicating which hosts can send requests. come.

By default, it is set to all Rails 6 applications and allows requests to the following hosts in the development environment IPAddr.new (“”), IPAddr.new (“:: / 0”), “localhost”] , its config can get RegExp , Proc , IPAddr and String IPAddr . This means that with Rails 6, we will need to explicitly put our domain name in the environment configuration files.

metadata purpose for signed / encrypt cookies

Rails is now able to prevent attacks by copying the signed / encrypt values ​​of one cookie and using it as the value of another.

Rails can do so by storing the cookie name in the purpose field and then signing / encrypt it along with the value of the cookie. Then, when read from the server side, the cookie name will be verified by purpose and then the hacked cookies will be discarded.

Enable config action_dispatch.use_cookies_with_metadata to use this feature.

follow_redirect! more parameters to implicit GET requests through # follow_redirect!

We can now pass parameters to implicit GET requests by adding parameters to the follow_redirect! Method follow_redirect! .

Action View

Add allocations to the render template parse

Rails 6 will report on object allocation performed in views, this will let developers know how much time is spent allocating and collecting objects in the memory of the processes.

Active Job

Add the instrumentation hooks enqueue_retry.active_job , retry_stopped.active_job , and discard.active_job

Rails has a rich amount of instrumentation hooks integrated into Active Support and it spreads across the entire framework.

It allows you to listen for events occurring throughout the life cycle of requests, SQL queries, and Jobs.

With the addition of enqueue_retry.active_job , retry_stopped.active_job and discard.active_job make it even easier to analyze and handle Jobs based on their status.

Allows passing # retry_on and # discard_on multiple exceptions

Active Model

Allows renaming of properties when using #has_secure_password

Previously, the default attribute when using #has_secure_password was ‘pasword’.

Active Storage

File uploaded when assigned to a record will be saved to storage when the record is saved instead of being saved to storage immediately

In Rails 5.2, files will be immediately saved to storage when assigned

Now its mechanism is more accurate to save only when @user.save

Using the ImageProcessing gem for Active Storage and its variants, deprecate the MiniMagick

ImageProcessing supports some better macros like :resize_to_fit :resize_to_fill and has built-in libvips replacement for ImageMagick.

It’s also easy to make changes by configuring Rails as usual

Action Mailer

Add MailDeliveryJob , deprecate DeliveryJob and Parameterized::DeliveryJob

MailDeliveryJob is used for both sending regular and parameterized mail


Zeitwerk is a new code loader for Ruby. It is efficient, thread-safe, and consistent with Ruby’s constant syntax.

With a well-defined directory structure, Zeitwerk will load classes and modules as needed, meaning you don’t need to write require statements in your file.

Zeitwerk is enabled by default in Rails 6.

However, if you don’t want to use it you can always use the old loader by config in application.rb .

Source: https://medium.com/rubyinside/whats-coming-to-rails-6-0-8ec79eea66da

Share the news now

Source : Viblo