Rails 6.1 RC2: Horizontal Sharding, Multi-DB Improvements, Strict Loading, Destroy Associations in Background, Error Objects, and more!

Posted by rafaelfranca, December 1, 2020 @ 10:30 pm in Releases

The second release candidate for Rails 6.1 has been released and brings a more robust experience for those already trying this version. We’ve been hard at work tweaking and adjusting the nobs to have so using this version is a smooth ride to everyone. The final release is scheduled to happen next week.

It’s amazing how Rails has grown over the years and while we have some improvements to make to the onboarding process, Rails has never been better. The features in this release focus on adding the functionality you need to keep your application up and running for years to come.

Let’s look at some of the new functionality:

Multi-DB Improvements

Per-database Connection Switching

Rails 6.1 provides you with the ability to switch connections per-database. In 6.0 if you switched to the reading role then all database connections also switched to the reading role. Now in 6.1 if you set legacy_connection_handling to false in your configuration, Rails will allow you to switch connections for a single database by calling connected_to on the corresponding abstract class.

Horizontal Sharding

Rails 6.0 provided the ability to functionally partition (multiple partitions, different schemas) your database but wasn’t able to support horizontal sharding (same schema, multiple partitions). Rails wasn’t able to support horizontal sharding because models in Active Record could only have one connection per-role per-class. This is now fixed and horizontal sharding with Rails is available.

Additional Improvements

In addition to adding horizontal sharding support we added tons of new functionality and improved a lot of internals for multiple databases. Kyle Thompson added support for database namespaced tasks like db:schema:dump:namespace, db:schema:load:namespace, db:structure:dump:namespace, and db:structure:load:namespace. Jean Boussier from Shopify improved connection pool management.

Strict Loading Associations

In addition to the many database and connection management improvements, Aaron Patterson and Eileen M. Uchitelle added support for strict loading associations. With this feature you can ensure that all your associations are loaded eagerly and stop N+1’s before they happen. Kevin Deisz added additional support to association declarations and bogdanvlviv added support to turn strict loading on by default.

Delegated Types

Rails 6.1 adds “Delegated Types” as an alternative to single-table inheritance. This is helpful for representing class hierarchies allowing the superclass to be a concrete class that is represented by its own table. Each subclass has its own table for additional attributes. Check out the pull request written by DHH.

Destroy Associations Async

Destroy associations async adds the ability for applications to destroy associations in a background job. This can help you avoid timeouts and other performance issues in your application when destroying data. The implementation was a group effort - the PR was started by George Claghorn from Basecamp, further support added by Cory Gwin of GitHub and finalized by Rafael França and Adrianna Chang from Shopify.

Error Objects

Active Model’s errors are now objects with an interface that allows your application to more easily handle and interact with errors thrown by models. The feature was implemented by lulalala and includes a query interface, enables more precise testing, and access to error details.

Active Storage Improvements

Active Storage got a nice update in Rails 6.1! You can now configure attachments for service you want to store them in. The feature was implemented by Dmitry Tsepelev.

Additionally, Rails 6.1 adds support to Active Storage for permanent URLs for blobs. Implemented by Peter Zhu from Shopify, this feature allows configuring your attachments to use a private or public URL and ensures that public URLs will always use a permanent URL.

Disallowed Deprecation Support

If you like to run your application deprecation-warning free then this feature is for you. The feature allows applications to optionally raise an error if a deprecation warning is thrown. This is useful for making sure developers don’t reintroduce deprecation warnings that have already been fixed. The feature was implemented by Cliff Pruitt of Test Double!

Performance Improvements and Bug Fixes!

A release isn’t just about the awesome features you get. It’s also about fixing bugs, improving performance, and making Rails more stable for everyone. This release includes an improvement that avoids making a query if where passes an empty array reported by Molly Struve and the fix implemented by John Hawthorn. Eileen M. Uchitelle and Aaron Patterson also implemented a performance improvement that speeds up where queries when we know all the values are an integer.

The classic Autoloader is Deprecated

The classic autoloader has served us well since the first Rails release, but there’s a new kid on the block and it is going to start its deprecation cycle.

New Rails projects are strongly discouraged from using the classic autoloader, and we recommend that existing projects running on classic switch to zeitwerk mode when upgrading. Please check the Upgrading Ruby on Rails guide for tips.

And more!

There are so many great changes in Rails 6.1. 686 people made contributions to Rails. Check out the CHANGELOGS for more details on bug fixes, performance improvements, and other features.

Thank you to everyone who reported a bug, sent a pull request, and helped improve Rails. Rails is better because of your hard work!

We hope you test out Rails 6.1 and love it as much as we do. Please report any bugs to the Rails issue tracker.