Rails 6.1 has been released and wow does it have a lot of great stuff! We’ve been hard
at work these past few months implementing improvements to multiple databases, adding support for destroying
associations in jobs instead of in-memory, turning errors into objects, and so much more.
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:
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
false in your configuration, Rails will allow you to switch connections for a single database by calling
connected_to on the corresponding abstract class.
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.
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: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.
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.
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.
classic Autoloader is Deprecated
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.
There are so many great changes in Rails 6.1. 654 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!