With the official release of Rails 6 just around the corner, we round up all the major new features coming your way. It is an exciting release due to some big features coming upstream from the Basecamp and GitHub projects. Amongst the many minor updates, useful tweaks and bug fixes, Rails 6 will ship with two completely new frameworks: ActionText and ActionMailbox, and two big scalable-by-default features: parallel testing and multiple database support. So set your Gemfile to get Rails 6.0.0.rc1 and let’s get started!
Multiple Database Support
Rails now supports switching between multiple databases. This is a very small change to the codebase, but it’s a feature that many developers are going to find really useful. The contribution came upstream from the GitHub developers, and provides an API for easily switching between multiple databases.
Possible use cases for this new feature would be having a read-only version of your database to use in areas known for having slow queries. Or another example would be if you had the need to write to different databases depending on which controller is executing.
First of all, you need to add the extra databases to your database.yml config file like this:
development:
main:
<<: *defaults
database: main_db
slow_queries:
<<: *defaults
database: readonly_main_db
You can then specify at the model-level which database(s) you want to use:
class User < ApplicationRecord
connects_to database: { writing: main, reading: slow_queries }
end
And then it’s just one line of code to temporarily switch between databases inside a block!
User.connected_to(role: :reading) do
# Do something that you know will take a long time
end
ActionText
ActionText is one of two brand-new frameworks extracted out of Basecamp and coming to Rails 6 (the other being ActionMailbox). It brings rich-text support to your rails applications, all made possible with the very stylish Trix editor. All you have to do is add a line code to your model (has_rich_text :column_name) and then you can use the ‘rich_text_area’ field in your view.
The Trix editor will capture rich-text information (such as bold text, headings, images etc.) and save this data into your desired storage solution, along with saving associated metadata into some new tables (note that this means that ActionText requires you to be using ActiveStorage).
ActionMailbox
ActionMailbox is an exciting new framework that allows you to route incoming mail into controller-like mailboxes for processing. This is another feature that comes extracted out of Basecamp, ready to use in Rails 6. Incoming mail is stored in a database table called InboundEmail and ActiveJob is utilized to tie the automation together.
There are lots of exciting possible use cases for this, and I’m sure many ideas will pop into your head when you start to think about it. For example, when a user replies to an automated email that your application sent notifying them about a comment. The user could reply to that email, and your application could process that email and turn it into a reply comment in your application automatically. As you can see, this is very powerful and flexible feature that we can’t wait to get our hands on.
Parallel Testing Support
Another scalable-by-default feature coming to Rails 6 is support for parallel testing. By running the test suite on multiple threads (each with their own version of the test database), this new feature will enable faster test suite run times and efficiency. Maybe not the most exciting feature, but certainly one which large projects will be very thankful for.
You can specify the number of threads to utilise in an environmental variable. This is useful for working with your CI system, which would typically have a different set-up from your local machine.
PARALLEL_WORKERS=3 rails test
Alternatively, to make universal changes, you can add this line of code to the parent test class:
class ActiveSupport::TestCase
parallelize(workers: 3)
end
Webpack
It’s nice to see Rails move with the times and never be afraid to abandon ideas that have grown irrelevant. These days, many projects are using front-end Javascript frameworks instead of the default Rails view layer. This has resulted in the existing Rails Asset Pipeline no longer being a good match for many people.
Whereas Webpack on the other hand, has become the industry standard in the front-end community in recent years, and so it makes sense to move towards that architecture going forward. The Webpacker gem has been around for some time, but now in Rails 6 it will become the default solution for Javascript asset bundling in Rails.
Zeitwerk
Last but surely not least, here’s an interesting one to keep an eye on: Zeitwerk, a new and improved, thread-safe code loader for Rails. This replaces the existing code loader which has been around since 2004, which has done a noble job, but has some major limitations.
What’s next?
Thank you for reading this far. Please help yourself to some freebies by clicking on doggo below. As you can see, there is lots to be excited for with the release of Rails 6. And besides these major new features, there are many more minor features and fixes too, so be sure to take a read through the documentation before you upgrade. So why not try out the new Rails 6.0.0.rc1 gem on a test project today?
And whilst we are talking about trying new things, have you tried using Scout for you application performance monitoring yet? If not then sign up for a free trial of Scout today to start monitoring you Rails 6 apps. Or contact us on support any time to discuss your application performance monitoring needs.
matthew@scoutapp.com
https://www.scoutapm.com/wp-content/uploads/2024/09/InT1pItpQqOATH3FPoXq.png