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

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.

New Active Record and Action View capabilities, bug fixes and more!

Hello! Andy here, highlighting some of the contributions to Ruby on Rails over the last week. Let’s dive in!

Add where.associated to check association presence

The Changelog covers how to use where.associated to check for the presence of an association. This also mirrors where.missing.

Include stylesheets, JS and ERB in stats

With this change, stats from the app/views and app/assets/stylesheets directories are now included with rails stats.

Add support for eager loading all rich text associations

Add the with_all_rich_text method to eager load all rich text associations on a model at once.

Bugfix: Preserve application time zone with travel_to

When parsing a time value as a string with travel_to, preserve the application’s time zone.

Transform Hash into HTML attributes for ERB

This change adds a new method to create HTML attributes from a Ruby Hash. Check the Changelog for an example.

We’re thankful for the contributions from 21 people to Rails over the last week. Have a look at the open issues and become a contributor. Until next time!

Bugfixes, improvements and more!

Hey, this is Greg, bringing you the latest news about Ruby on Rails!

Fix db:schema:load when table definition contains partitions

This PR fixes a bug with loading the schema to MySQL if the schema contains table definitions with partitions.

Add strict loading to Active Storage

The strict_loading option has been added to Active Storage, you can enable it like this:

has_one_attached :logo, strict_loading: true
has_many_attached :images, strict_loading: true

Fixed odd behavior of inverse_of with multiple belongs_to to the same class

An issue was introduced when foreign key validation was added to automatic_inverse_of and it is fixed by this PR. You can find an example and detailed explanation on the PR itself.

Use FFmpeg scene detection for video previews

This PR adds FFmpeg scene detection to Active Storage video previews. There are some test results on the PR to see how it works.

17 people contributed to Rails since last time, have a look at the open issues and become one of them! Until next week!

Enhanced strict loading, multiple databases and more!

Hi, Wojtek from this side with a summary of the latest changes that will be available in the upcoming Rails 6.1.

Rails 6.1 RC1 released

Release candidate of Rails 6.1 is ready for testing.

Enhance strict loading to log violations

This change allows for an application to optionally log instead of raise when using strict_loading.

Add connected_to_many for multiple databases

This would come in especially handy for deeper nesting past 2 databases when declaring connections.

Add request exclusion to host authorization

Host authorization checks can be skipped for specific requests. This allows for health check requests to be permitted for requests with missing or non-matching host headers.

Add server Railtie block

Allow the application or a railtie to load code after the server start (similar to console and tasks blocks).

39 people contributed to Rails since last time. Check out the detailed list of all changes. Until next week!

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

The first release candidate for 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:

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.

Multiple database improvements, bugfixes and more!

Hey, this is Greg bringing you the latest news about the Rails framework.

Show outstanding migrations on the error page

This PR adds the details of the pending migrations to the pending migrations error message so you will know from the error what’s pending.

Add ability to set per param encoding

Previously you could skip encoding which would encode all parameters on an action as ASCII_8BIT, but after this change you can specify the param_encoding for any one parameter on an action in your controller. An example can be found here.

Fix resources being fetched twice when crossorigin attribute is used

When you load a script or css (by using javascript_include_tag or stylesheet_link_tag respectively) with crossorigin attribute applied, Rails currently causes some browsers to fetch these resources twice. That is because crossorigin in the link header preload directive and on the resource itself need to match in order for browsers to re-use a resource.
This PR changes it so that the link header directives include the same crossorigin values as those that have been passed to the resources themselves, which allows browsers to reuse the preloaded resource.

Implement connecting_to method

Sometimes you need to have a different default connection but aren’t calling the connection with a block. An example is booting a console in reading mode. This PR adds the ability for a script to set a specific connection on boot while preserving the behaviour of connected_to for application code.

Warn if we can’t read the yaml to create database tasks

For multiple databases Rails attempts to generate the tasks by reading the
database.yml before the Rails application is booted but there are some complex cases when this is not possible and Rails will simply issue a warning saying it couldn’t infer the database tasks from the database.yml.

Fix TimeWithzone bug

There was a rounding off issue when we were comparing TimeWithZone times with DateTime and this PR fixes that.

29 people contributed to Rails since last week. Check out the detailed list of all changes. Until next week!

Active Record values_at and cache improvements

Hi, it’s Wojtek bringing you the latest changes summary from Ruby on Rails codebase.

Add values_at method to Active Record

Simplifies retrieving values of specific attributes on Active Record model instance, ie person.values_at(:name, :age).

Use environment variable MEMCACHE_SERVERS by default

When no specific configuration is provided, MEMCACHE_SERVERS environment variable will be checked first before falling back to localhost 127.0.0.1:11211 address in ActiveSupport::Cache::MemCacheStore.

Add store name to cache instrumentation

When subscribing to a “cache” instrumentation events, additional parameter with store name is provided to ease recognizing the context.

12 people contributed to Rails since the last week. Check out the detailed list of all changes. Until next week!

New API Docs site, configurable cache coder, bug fixes, and more!

Hi there! This is Andy bringing you the latest on what’s shipped this week in Ruby on Rails!

New API docs back-end

The frames-based Rails API docs site has been replaced with CSS and Turbolinks. Deep linking is now supported! Let’s take a moment and remember the humble 1990s era <frame/>! Check it out at https://edgeapi.rubyonrails.org.

Stable sorting for DatabaseConfigurations#find_db_config

Sorting configuration for multiple databases was not guaranteed to be stable. This PR fixes that, and includes a supporting test with more information.

Handle binary strings in Active Record serialized columns

When binary data is stored in a serialized column, this change ensures that the original value is preserved. Check out the supporting test for an example.

Make ActiveSupport::Cache coder configurable

This PR lays the groundwork to specify a custom coder. The PR author describes a scenario migrating between 2 different cache stores as a potential use case.

Include layout when rendering objects from controllers

Did you ever notice Rails was like “nah”, when trying to render from a controller with a layout? Well, I’ve got some good news for you.

Fix read_attribute_before_type_cast

From the author: “With this change, read_attribute_before_type_cast will be able to get the value before typecast even if the attr_name is an attribute_alias.”

Fixes, optimizations and documentation

And finally, let’s recognize these contributors that fixed a flaky test, created an optimization to avoid unnecessary queries and another that avoids a duplicate record. In addition to that, 5 PRs shipped this week with documentation improvements! Thank you!

Thank you to the 20 people that contributed to Rails this week. If you’d like to be part of that, check out the list of open issues. Until next time!

A security release, bugfixes and more!

Hey, this is Greg, bringing you the latest news about Ruby on Rails!

Rails 6.0.3.4 has been released!

Rails 6.0.3.4 has been released! It is a security release and addresses a possible XSS attack vector in Actionable Exceptions.
You can read more about the issue here and check the commit with the fix here.

Optimize ActiveRecord::Relation#include? on an unloaded relation

This PR introduces an exists? call instead of loading the entire relation into memory and that makes it better performing.

Fix ActiveRecord::Relation#include? in case where offset is provided

A follow-up PR for the above improvement, because the original solution broke includes? when an offset was provided, but this change covers that case and falls back to loading the relation in case an offset is provided.

Support passing record to uniqueness validator’s :conditions option

With this change, it is possible to build conditions based on the record’s attributes for a uniqueness validation.

Allow for only no-store in cache-control header

This PR allows one to set the default Cache-Control header to reflect the simple no-store directive exclusively and all other cache directives are dropped when that’s set.

20 people contributed to Rails the past week! If you want to be part of that, check out the list of open issues! Until next week!

Rails 6.0.3.4 has been released

Hey everyone! Rails version 6.0.3.4 has been released! This version is a security release and addresses one possible XSS attack vector in Actionable Exceptions.

You can read more about the issue here.

Here are the checksums for the gems:

$ shasum *6.0.3.4*
04ec20fe5d23d9e21ed2dd04139563e4173f6c1b  actioncable-6.0.3.4.gem
c96926a293abcb63a9471e9064824160b7348683  actionmailbox-6.0.3.4.gem
e2a0221f78f260b93ac57c1d40f2c0cabe82b1c9  actionmailer-6.0.3.4.gem
83a4fc7ebb7e7b41795149e1d1879e2bce4540f2  actionpack-6.0.3.4.gem
b080d368861b406b2b78ae46f905476d66b8ba4b  actiontext-6.0.3.4.gem
27ecb9befa64104c7b27bc547f4f58de4ba25d9c  actionview-6.0.3.4.gem
5405baf9298cc3af01a5160ebed4ebb79eec0a69  activejob-6.0.3.4.gem
777cf1db073a988d47e1669f494801065560772d  activemodel-6.0.3.4.gem
1f8b15b082531461d7fa029e238b4a53034ddb5c  activerecord-6.0.3.4.gem
07d31d2e1c690384aa6e196c9e844f953551918b  activestorage-6.0.3.4.gem
0c145c62c94dae5c36641d0abcd0026ff40cba95  activesupport-6.0.3.4.gem
a1503f158074aae4dce10d9c1ef6110a3cb61657  rails-6.0.3.4.gem
6066d3a3975184aa566c803aa6c573086f0b296e  railties-6.0.3.4.gem

Have a great day!

<3