Skip to content

Include valid values in ActiveRecord::Enum invalid-value error - #57446

Merged
rafaelfranca merged 1 commit into
rails:mainfrom
hammadxcm:enum-error-message-list-valid-values
May 22, 2026
Merged

rafaelfranca merged 1 commit into
rails:mainfrom
hammadxcm:enum-error-message-list-valid-values

Conversation

@hammadxcm

Copy link
Copy Markdown
Contributor

Motivation

When assigning a value that isn't a member of an enum, the ArgumentError Rails raises gives no hint at what is valid:

Book.new(status: "bogus")
# ArgumentError: 'bogus' is not a valid status

For any model with more than a couple of enum members, the developer then has to open the model definition just to find out which value to use. Listing the valid values directly in the error message is a meaningful debugging UX improvement and zero behavior risk — same exception class, same trigger.

After

Book.new(status: "bogus")
# ArgumentError: 'bogus' is not a valid status. Valid values are: "proposed", "written", "published"

Implementation

One-line change in activerecord/lib/active_record/enum.rb — appends the mapping keys to the existing message. Uses Symbol#inspect so the strings render with quotes and remain unambiguous regardless of whether the enum was defined with symbols or strings.

Tests

Exactly one test in enum_test.rb asserted the previous message verbatim. It now uses two assert_match checks: one for the unchanged prefix, one confirming a known valid value ("proposed") is present. This keeps the test robust if a future contributor reorders the mapping.

CHANGELOG

Top entry added in activerecord/CHANGELOG.md with a before/after example.

Verification

$ ARCONN=sqlite3_mem bundle exec ruby -Itest test/cases/enum_test.rb
100 runs, 477 assertions, 0 failures, 0 errors, 0 skips
$ bundle exec rubocop activerecord/lib/active_record/enum.rb activerecord/test/cases/enum_test.rb
2 files inspected, no offenses detected

@hammadxcm

Copy link
Copy Markdown
Contributor Author

@rafaelfranca Please have a look on this one at you FREE time.

When assigning a value that is not a member of an enum, the
`ArgumentError` previously gave no hint at what *is* valid:

    Book.new(status: "bogus")
    # ArgumentError: 'bogus' is not a valid status

It now lists the accepted values, which is a meaningful debugging
aid for any application with more than a handful of enum members:

    Book.new(status: "bogus")
    # ArgumentError: 'bogus' is not a valid status. Valid values are: "proposed", "written", "published"

The single test asserting the exact message switches to `assert_match`
so it stays robust to mapping-order changes.
@rafaelfranca
rafaelfranca force-pushed the enum-error-message-list-valid-values branch from b853408 to fcf4393 Compare May 22, 2026 22:15
@rafaelfranca
rafaelfranca merged commit 53fc866 into rails:main May 22, 2026
2 checks passed
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