This Week in Rails

June 21, 2024

New database sharding methods, improved Active Record Migration Docs, caching improvements for ActiveStorage and more!

Hey everyone, Happy Friday! Vipul here with the latest updates for This Week in Rails. Let’s dive in.

Improved Active Record Migration Documentation
The Rails Foundation documentation team continues to improve different areas of the Guides. This Pull Request updates the Active Record Migration Documentation to receive various additions, improvements and more.

Add .shard_keys, .sharded?, & .connected_to_all_shards methods to ActiveRecord::Base
This change adds .shard_keys, .sharded?, & .connected_to_all_shards methods to start returning sharding information for a model.
class ShardedBase < ActiveRecord::Base
  self.abstract_class = true

  connects_to shards: {
    shard_one: { writing: :shard_one },
    shard_two: { writing: :shard_two }
  }
end

class ShardedModel < ShardedBase
end

ShardedModel.shard_keys => [:shard_one, :shard_two]
ShardedModel.sharded? => true
ShardedBase.connected_to_all_shards { ShardedModel.current_shard } => [:shard_one, :shard_two]    


Expire caching when a download fail while proxying in ActiveStorage
The Proxy controllers in Active Storage set the caching headers early before streaming. In some instances, it was possible for the file download to fail before we send the first byte to the client. In those instances, this change would invalidate the cache and return a better response status before closing the stream.

Lazily generate assertion failure messages
Minitest supports passing the failure message as a callable, which allows us to defer generating failure messages. Some of these failure messages can be a bit costly to generate, particularly when inspecting very large objects or when accessing the AST of procs. This change now defers the generation of the failure message by passing it in a callable instead.

Make “g scaffold” with no field produce rubocop compliant code
When there are no fields and we use rails g scaffold, the generated code is not rubocop compliant.
This change makes it compliant by:
  • Omitting a blank line in migration prior to “t.timestamps”.
  • Omitting leading and trailing spaces in empty hashes in create and update controller and api functional tests.

Fix alias_attribute to ignore methods defined in parent classes
When defining regular attributes, inherited methods aren’t overridden. However, when defining aliased attributes, inherited methods aren’t considered. This change fixes alias_attribute to properly ignore methods defined in parent classes.

You can view the whole list of changes here.
We had 26 contributors to the Rails codebase this past week!

Until next time!

About This Week in Rails

Your weekly inside scoop of interesting commits, pull requests and more from Rails.