#58523·rails

Rails 8.1 breaks certain comparisons for time fields

Author: daniel-sjoblom-europcarCreated Aug 20, 2026Updated Sep 28, 2026

Steps to reproduce

ruby
# frozen_string_literal: true

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"

  gem "rails", "~> 8.1.0"

  gem "sqlite3"
end

require "active_record/railtie"
require "minitest/autorun"

# This connection will do for database-independent bug reports.
ENV["DATABASE_URL"] = "sqlite3::memory:"

class TestApp < Rails::Application
  config.load_defaults Rails::VERSION::STRING.to_f
  config.eager_load = false
  config.logger = Logger.new($stdout)
  config.secret_key_base = "secret_key_base"

  config.timezone = 'Europe/Helsinki'

  config.active_record.encryption.primary_key = "primary_key"
  config.active_record.encryption.deterministic_key = "deterministic_key"
  config.active_record.encryption.key_derivation_salt = "key_derivation_salt"
end
Rails.application.initialize!

ActiveRecord::Schema.define do
  create_table :things, force: true do |t|
    t.time "from_attr"
    t.time "to_attr"

    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end
end

class Thing < ActiveRecord::Base
  validate do
    errors.add(:base, :invalid) if to_attr < from_attr
  end
end

class BugTest < ActiveSupport::TestCase
  def test_time_comparison
    from = Time.local(2000, 1, 1, 1, 0)
    to = Time.local(2000, 1, 1, 23, 0)

    Thing.create!(from_attr: from, to_attr: to)
  end
end

Expected behavior

The test should pass cleanly. Note that if you change the Rails version in the script to 8.0.0, it passes as expected.

Actual behavior

The test crashes on the save due to the validation failing. This is only in Rails 8.1. Based on my investigation, it seems to be caused by https://github.com/prateekkish/rails/commit/ef2f0ce24b407de317c18b5b11622c12aa9cafdf, for https://github.com/rails/rails/issues/55118.

System configuration

Rails version: 8.1.3.1 (not ok) and 8.0.5.1 (ok)

Ruby version: 3.4.10