#53448·rails

活动记录 :: Associations.handle dependency downcase 在错误消息中属性名称

作者: mitya-murakhouski创建于 2024年10月25日更新于 2026年9月11日
标签attached PR

重现步骤

ruby

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'

此连接用于无数据库的错误报告。

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! # 尝试删除 post,这应该失败,因为存在依赖的 Some Comments。 assert_not post.destroy # 验证 post 的错误中是否添加了适当的错误消息。错误消息应该包含自定义翻译中定义的可读人类名称 'Some Comments'。 assert_equal [ 'Cannot delete record because dependent Some Comments exist', ], post.errors[:base] end end