Repository navigation
Include valid values in ActiveRecord::Enum invalid-value error - #57446
Merged
rafaelfranca merged 1 commit intoMay 22, 2026
Merged
Conversation
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
force-pushed
the
enum-error-message-list-valid-values
branch
from
May 22, 2026 22:15
b853408 to
fcf4393
Compare
rafaelfranca
approved these changes
May 22, 2026
3 of 4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
When assigning a value that isn't a member of an enum, the
ArgumentErrorRails raises gives no hint at what is valid: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
Implementation
One-line change in
activerecord/lib/active_record/enum.rb— appends the mapping keys to the existing message. UsesSymbol#inspectso 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.rbasserted the previous message verbatim. It now uses twoassert_matchchecks: 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.mdwith a before/after example.Verification