Hello 2021: Sole record, Same form multiple verbs, Validator with range, Rich text control

Hi there. Andy here, with the first newsletter of 2021, highlighting some updates to Ruby on Rails over the last few weeks. Let’s jump right in!

Find and assert the presence of exactly one record

Add FinderMethods#sole and #find_sole_by to find and assert the presence of exactly one record.

Submit the same form with different HTTP methods

This change provides the ability to post the same form with different HTTP methods. For example PUT and a DELETE requests can be made from the same form.

Validate numericality with a Range

A Ruby range can now be provided as part of a numericality validator.

Improve Action Text extensibility

Allow the HTML surrounding rich text to be customized while retaining private control over how the rich text itself is rendered.

Consistently render button_to

Change Action View helpers to always render a <button> element.

Add stats to Redis Cache Store

Add a RedisCacheStore#stats method that is similar to MemCacheStore#stats.

The option config.action_view.preload_links_header was added to allow disabling of the Link header when using stylesheet_link_tag and javascript_include_tag.

Fix: Respect getter overwrites in the model

ActiveRecord::AttributeMethods::Query will now respect the getter overrides defined in the model.

45 people contributed to Rails over the last several weeks. Check out the open issues to get involved and help out. Until next week!

Rails 6.1.1 has been released

Hi everyone,

I am happy to announce that Rails 6.1.1 has been released.

CHANGES since 6.1.0

To view the changes for each gem, please read the changelogs on GitHub:

To see a summary of changes, please read the release on GitHub:

6.1.1 CHANGELOG

Full listing

To see the full list of changes, check out all the commits on GitHub.

SHA-256

If you’d like to verify that your gem is the same as the one I’ve uploaded, please use these SHA-256 hashes.

Here are the checksums for 6.1.1:

$ shasum -a 256 *-6.1.1.gem
bc156d04d7a2f5d0702e319a3e01751ccff27e54a627a1beebb3cb89dff1aefe  actioncable-6.1.1.gem
34146a9e7a3ea23824c53f42593d6a621ffc653686c186f9a379fce30737e2fa  actionmailbox-6.1.1.gem
9c4394d61b22d791c38dcf026b91039e1b5a86ea634b67fe2cd9b7cc06168f17  actionmailer-6.1.1.gem
3ecfe3b11e31e4a9e6db771244085c7783b482b82380fdce52abd11dab1153e3  actionpack-6.1.1.gem
6e749845fca914cf0ba4358d96eb8d8428b903a0192442aa9c3edfdace6c996c  actiontext-6.1.1.gem
dc9e6c2a15e66601822d089c7f4322c2a44fa86ae6c53bea57ef0bcdd8597e9f  actionview-6.1.1.gem
f35e026b49d9f53b70aece18846a641446f0bc3f78640612806330d55502a189  activejob-6.1.1.gem
b2b9d455446097e36968c009644a3c38345d1cbc449028e20a4bd43fb63fd12f  activemodel-6.1.1.gem
d10fa2c7a91b3b24a7b42b47590d3d5c46b80eb49a63c184a6cd1743ee31e9da  activerecord-6.1.1.gem
798b2806b3f485a7b65e8b39944a65ef216d39f92dfa3e7dcd0d829e5051a4b8  activestorage-6.1.1.gem
a1d7d91586b2bbebb93f931802e5b1ceab565137d0769c55d9941052de85ea22  activesupport-6.1.1.gem
b7710f82e68af72db1ffa30ff3d67437e8fb91c26255659f3c2602964b834a64  rails-6.1.1.gem
fa0818457755b22de1fc673d8e924300f8f730fc4c2a223bd9a94837f7ce81c5  railties-6.1.1.gem

As always, huge thanks to the many contributors who helped with this release.

This week in Rails: benchmark anywhere, bugfixes and more!

Hi there, it is Greg, bringing you the latest news about Rails!

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

Rails 6.1 has been released and wow does it have a lot of great stuff! The changes include improvements to multiple databases, adding support for destroying associations in jobs instead of in-memory, turning errors into objects, and so much more.

Add benchmark method that can be called from anywhere

This PR includes the existing ActiveSupport::Benchmarkable into the Rails module, to make it accessible from anywhere (background jobs etc).

Add config.action_view.image_loading

Browser native support for lazy loading images is now a part of the official HTML standard and this PR adds Rails.application.config.action_view.image_loading to configure sitewide default for the loading html attribute to enable lazy loading sitewide without changing code.

Add config.action_view.image_decoding

Another image_tag change, which introduces Rails.application.config.action_view.image_decoding to configure the default value of the image_tag :decoding option.

Better handling of negative elements in enum

Rails gives a warning if an enum method uses the not_ prefix as it might conflict with negative scopes. The initial implementation warned even when there was no actual conflict and this change improves that and only warns about negative enums if a positive form that would cause conflicts exists.

Do not use submit_tag auto-disabling when disable_with is set to false

If we have data: { disable_with: false } then auto-disabling is turned off, but if we set automatically_disable_submit_tag to false it changes the behaviour of disable_with in an unexpected way,  so explicit usage of disable_with: false starts to enable auto-disabling with false as the value of a disabled button.
With this change auto-disabling is turned off if we have explicit disable_with: false no matter what is set in automatically_disable_submit_tag.

Ignore strict loading violations on instances loaded through fixtures

Rails 6.1 added _strict_loading_by_default _but that causes issues with the fixtures as they are lazy loaded. To get around it, this change ignores the strict loading flag when the fixtures are loaded.

Fix S3 multipart uploads when threshold is larger than the file uploaded

A bug happened when the file being uploaded to S3 is smaller than the configured multipart threshold, but this PR fixed it.

Allow reload to be default_scoped

reload was not default_scoped by default because you could be creating a record that does not match your default scope and therefore reload wouldn’t find the record. However, in the case of sharding an application you may want reload to
support default_scope because you’ll always have the correct scope
set. 

Change default queue name of all the internal jobs to be the job adapter’s

Before this change you needed to configure your job processor to handle all of Rails’ internal job queues, but not anymore!

Handle nil translation key

In Rails 6.0, the translate helper always returned nil when given a nil key, but in Rails 6.1, the translate helper always raised an I18n::ArgumentError when given a nil key. This PR fixes the translate helper to mirror the I18n.translate behaviour when given a nil key, with and without a default.

45 people contributed to Rails in the past 2 weeks. Check out the open issues if you want to be one if them next time!. Until next week!

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

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. 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!

Rails 6.1 RC2 and some enhancements

Hi, Wojtek from this side with a small set of changes since the last week.

Rails 6.1 RC2 released

The second release candidate for Rails 6.1 has been released and brings a more robust experience for those already trying this version.

Add option for “default_scope” to run on all queries

This change allows for applications to optionally run a default_scope
on update and delete queries in addition to current behavior on get and insert statements, by adding all_queries: true.

Add “id” and “field_id” methods to FormBuilder

Ease generating consistent DOM ids in given form context.

New default for “local” option on “form_with”

With Rails 6.1 version form_with will generate non-remote forms by default.

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

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.