Rails 7.0.7 Improves In_order_of Method For Enum Fields
Rails 7.0's method simplifies sorting enum fields by status in alphabetical order, handling NULL values correctly from Rails 7.0.7 onwards.
The problem It's a common case for Rails applications to have enum fields on a model like: class Review < ApplicationRecord enum status: [:pending, :processing, :completed] end All's good until we need to sort records by their status in a specific order. For example, on reviews page records should be ordered by status in alphabetical order, but internally in DB status values are stored as integers. We can't just add order(status: :asc). Previously we'd have to write some custom SQL for the task: Review.order(" CASE status WHEN 2 THEN 1 WHEN 0 THEN 2 WHEN 1...