Skip to content

Require String#remove in ActiveModel::Error - #59946

Merged
fatkodima merged 1 commit into
rails:mainfrom
wintan1418:require-string-filters-in-active-model-error
Nov 24, 2026
Merged

fatkodima merged 1 commit into
rails:mainfrom
wintan1418:require-string-filters-in-active-model-error

Conversation

@wintan1418

Copy link
Copy Markdown
Contributor

Motivation / Background

This Pull Request has been created because ActiveModel::Error.full_message and ActiveModel::Error.generate_message call String#remove, which is defined in active_support/core_ext/string/filters, but active_model/error.rb doesn't require it.

Including ActiveModel::Validations loads it through another file, so it doesn't show up there or in the Active Model test suite. A model that only uses ActiveModel::Errors, as in the minimal implementation in the ActiveModel::Errors docs, raises from full_messages when an attribute name has a dot:

require "active_model"

class Person
  extend ActiveModel::Naming

  def initialize
    @errors = ActiveModel::Errors.new(self)
  end

  attr_reader :errors

  def read_attribute_for_validation(attr)
    send(attr)
  end

  def self.human_attribute_name(attr, options = {})
    attr
  end

  def self.lookup_ancestors
    [self]
  end
end

person = Person.new
person.errors.add(:"address.street", "can't be blank")
person.errors.full_messages
# NoMethodError: undefined method 'remove' for an instance of String
#   activemodel/lib/active_model/error.rb:54:in 'ActiveModel::Error.full_message'

With extend ActiveModel::Translation, generate_message raises the same way for an attribute with a bracket, like errors.add(:"tags[0]", :blank).

On 8.1 (and 7.1) full_message calls remove for every attribute, so the example in the ActiveModel::Errors docs raises as written. I reproduced that with the released gems:

$ ruby -e 'gem "activemodel", "8.1.4.1"; gem "activesupport", "8.1.4.1"; load "errors_docs_example.rb"'
activemodel-8.1.4.1/lib/active_model/error.rb:51:in 'ActiveModel::Error.full_message': undefined method 'remove' for an instance of String (NoMethodError)

Detail

This Pull Request adds the missing require, the same fix #57310 made for ActiveModel::SchematizedJson.

Additional information

The snippets above print ["address.street can't be blank"] and ["Tags[0] can't be blank"] with the change. I didn't add a test because something in the Active Model suite always loads String#remove first; I checked the snippets in separate processes instead.

Tests run: activemodel: bin/test (1271 runs, 0 failures). bundle exec rubocop activemodel/lib/active_model/error.rb: no offenses.

No CHANGELOG entry as it is a minor fix, like #57310 and #59899. Happy to add one if preferred.

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

`ActiveModel::Error.full_message` and `.generate_message` call
`String#remove`, which is defined in
`active_support/core_ext/string/filters`, but error.rb doesn't require
it. Unless something else has loaded it (including
`ActiveModel::Validations` does), `full_messages` raises for an
attribute with a dot or a bracket in its name:

    require "active_model"
    # Person as in the ActiveModel::Errors docs, without Validations
    person.errors.add(:"address.street", "can't be blank")
    person.errors.full_messages
    # NoMethodError: undefined method 'remove' for an instance of String

On 8.1 and earlier `full_message` calls `remove` for every attribute,
so the minimal implementation in the `ActiveModel::Errors` docs raises
on `full_messages` as written.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants