This Week in Rails

October 28, 2023

Reduce memory used by ActiveSupport::Callbacks, non-column-backed attributes for enum and more!

Hi! This is Vipul bringing you the latest from this week’s changes in the Rails codebase.


Previously, callbacks which were shared with subclasses would not share between them the procs generated in before/after filters. This was also lazily generated so would cause memory growth after an application is booted.
This change adds reuse of these in before/after blocks halving the memory growth from boot to post-request-serving.

This re-adds support for using enum with non-column-backed attributes while still guarding against typos in enum attribute names. When using enum with a non-column-backed attribute, the attribute must be previously declared with an explicit type. For example:

class Post < ActiveRecord::Base 
  attribute :topic, :string 
  enum topic: %i[science tech engineering math]
end


This Pull Request adds support for number_to_human_size to handle negative numbers.

# Before
helper.number_to_human_size(-1234567)
# => "-1234567 Bytes"

# After
helper.number_to_human_size(-1234567)
# => "-1.18 MB"


Before this change, rails db:migrate:primary (and rails db:up:primary, rails db:down:primary) dumped schema files for all the databases, even though this is not necessary. This change fixes this before to only perform dump for specific database.

For example, when casting "3,50" works as expected instead of raising an error.

This change adds proper support for dup for BroadcastLogger class, where just calling dup wouldn’t suffice before.

This change fixes extract_value to also work and retain blank values.

params = ActionController::Parameters.new(tags: "ruby,rails,,web")
params.extract_value(:tags) # => ["ruby", "rails", "", "web"]


This helps to fix a bug where using url_helpers generated invalid urls if a records second composite primary key is empty.

When some invalid primary key values are passed to ActiveRecord::Persistence.delete method, Active Record still performed a DELETE query even if it is not needed. For example,

irb(main):001> User.delete([])
  User Delete All (0.6ms)  DELETE FROM "users" WHERE 1=0
=> 0


This change now skips unnecessarily running the query.
This change fixes using rails-ujs in bundled environments(with esbuild, etc), where rails-ujs would not auto start properly as its start method was getting called twice.

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

About This Week in Rails

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