Rails 6 was just released last month (August 2019). I have been using Rails professionally for more than a decade now, starting from Rails 2. Having worked on version 2 up to 5, this latest release is an exciting moment for me.

The full release notes are available here, but we will touch on some of the more notable changes and improvements that make Rails 6 a nice major version upgrade.

So long, Sprockets (at least in JS)

Webpack is now the default JavaScript compiler through the webpacker gem. However, the default CSS compiler is still Sprockets. Webpack already supports CSS compilation in addition to JavaScript, so you can opt to just use Webpack for everything by updating the webpacker configuration.

When you run rails new or run the server for the first time, you will notice that it runs webpack as part of the process. It saves the bundles under /public/packs and you can include it in your views and layouts using the helper javascript_pack_tag.

When you create a new Rails 6 application, it also includes a ready-made .gitignore file which includes the JS bundles, node modules, and files generated by yarn.

/public/packs
/public/packs-test
/node_modules
/yarn-error.log
yarn-debug.log*
.yarn-integrity

When you have JavaScript files inside app/assets, Rails detects any changes you make and automatically bundles the JS files when you process the request, so your changes are applied immediately in development. If you feel like this is still slow, you can also run a separate webpacker process and enable the “hot reload” magic to see your changes in the browser in real-time.

New Autoload library

Rails 6 now comes shipped with a new Ruby code loader library called Zeitwerk. This improves how loading is done which is summarized in the gem creator’s motivation:

Autoloading in Rails is based on const_missing, which lacks fundamental information like the nesting and the resolution algorithm that was being used. Because of that, Rails autoloading is not able to match Ruby’s semantics and that introduces a series of gotchas. The original goal of this project was to bring a better autoloading mechanism for Rails 6.


https://github.com/fxn/zeitwerk#motivation

This loading mechanism is enabled in Rails 6 by default and controlled by this configuration:

# config/application.rb
config.load_defaults "6.0" # enables zeitwerk mode in CRuby

In general, there is no change on how it works compared to previous versions. It still follows the same convention of naming your files (snake case) that corresponds to the class (camel case). You can even choose not to use Zeitwerk at all and just revert to the “classic” mode. The full guide on how this works can be found here.

Multiple Database Support

Rails 6 now provides an easy mechanism to connect to multiple databases in the application. This is still a pretty basic feature implementation as it only handles primary/replica databases, but this should pave the way for future features like sharding, load balancing and clusters.

This feature allows you to specify separate databases for your models as needed. As an example, you can create a separate database for the User table and connect your model like this:

class UsersBase < ApplicationRecord
self.abstract_class = true

connects_to database: { writing: :users, reading: :users_replica }
end

Rails assigns one database connection name per model, so to prevent hitting the limit on open connections to the database, you will need to define a base model first and then inherit from it.

class User < UsersBase
# User model
end
class Auth < UsersBase
# Authentication model
end

By default, writing is done on the primary database and reading is done on the replica database. There are still some details that need to be considered and this is discussed in the official guide.

Action Mailbox

If you have any experience handling inbound emails, this feature will resonate with you. Rails now provides a more structured way to handle incoming emails to your application. The incoming emails are stored in the database as InboundEmail, which will allow for better debugging and also provides an easy way to delete the email records as needed.

This feature comes with basic routing, which means you no longer manually need to parse the incoming email metadata to determine the appropriate action. The full guide can be found here.

Action Text

In my experience, what you see is what you get (WYSIWYG) editors can be more trouble than they are worth. However, due to improvements in browser technology and support, it looks like this is no longer the case nowadays.

Rails 6 now comes shipped with an editor called Trix, which is created by Basecamp itself. Since Basecamp’s product includes this particular feature, I am confident that Trix is a performant, battle-tested solution for modern editors. And since this is integrated with Rails, you can easily define specific columns to be displayed as rich text.

class Message < ApplicationRecord
has_rich_text :content
end

This means that you no longer need to manually handle persisting, sanitization and displaying the content in the view. More details are available here.

Parallel Testing

Who doesn’t want their tests to run multiple times faster? Rails 6 now supports parallel testing using two methods: forking and threading.

In a forked process, the tests will run depending on the number of workers you specify. For each worker, it will create a separate test database. For example, you have configured app-test-db as your test database in database.yml. When you run the tests using 2 workers, ActiveRecord will automatically create app-test-db-1 and app-test-db-2 for each process and load the schema accordingly.

If you are using JRuby, Rails will run parallel testing using threads by default. If you want to dig deeper, please refer to the official Rails testing guide.

Minimum database versions

In the interest of performance and security updates, the supported database versions are now set to the following:

  • PostgreSQL 9.3 and later
  • MySQL 5.5.8 and later
  • SQLite 3.8 and later

You may need to check the associated Ruby gem interface (pg, mysql2, sqlite3) to these databases to see if they are within the minimum supported versions.

Closing Thoughts

We only touched a few of the improvements done in the framework. These new features alone makes it worth upgrading to Rails 6. In my opinion, Rails still has its place in the fast-paced web development space. With years of industry usage, it is indeed way past its hype period, but this means it has now become a stable framework and a worthy part of a web developer’s toolbox.

Lastly, you know its good when Github itself is running on Rails 6 and has contributed significantly to its development!

One thought on “Hello Rails 6!

Leave a Reply

Your email address will not be published. Required fields are marked *