ActiveRecord::Associations.handle_dependency downcases attribute name in error messages
Author: mitya-murakhouskiCreated Oct 25, 2024Updated Sep 11, 2026
Labelsattached PR
Steps to reproduce
# frozen_string_literal: true
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'rails'
gem 'sqlite3'
gem('rails-i18n', '~> 7.0')
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
create_table :comments, force: true do |t|
t.integer :post_id
end
end
class Post < ActiveRecord::Base
has_many :comments, dependent: :restrict_with_error
end
class Comment < ActiveRecord::Base
belongs_to :post
end
class BugTest < ActiveSupport::TestCase
setup do
I18n.backend.store_translations(
:en,
{
activerecord: {
attributes: {
post: {
comments: 'Some Comments'
}
}
}
}
)
end
def test_association_stuff
post = Post.create!
post.comments << Comment.create!
# Attempt to destroy post, which should fail due to restrict_with_error
assert_not post.destroy
# Verify that the appropriate error message is added to the post’s errors.
# The error message should include the human-readable name 'Some Comments' as defined
# in the custom translation.
assert_equal ['Cannot delete record because dependent Some Comments exist'], post.errors[:base]
end
end
Expected behavior
Attribute name should remain the same as in translation
Actual behavior
Attribute name has been downcased from Some Comments to some comments
System configuration
Rails version: 6.1.7.9
Ruby version: 3.2.5
Source: rails/rails