Since Rails 7.1, enum accepts instance_methods: false to skip generating the auto-defined instance methods (#{name}? and #{name}! entirely. That’s useful, but it’s all-or-nothing; there’s no way to keep one of the two and drop the other.
class Conversation < ActiveRecord::Base
enum :status, [:active, :archived], instance_methods: { bang: false }
end
conversation = Conversation.new
conversation.active? # => false (still generated)
conversation.active! # NoMethodError (not generated)
- instance_methods: true / instance_methods: false keep working exactly as today.
- instance_methods: { predicate: false } drops only #{name}?.
- instance_methods: { bang: false } drops only #{name}!.
- Unknown hash keys raise ArgumentError, consistent with the existing enum option validation.
I have a small patch against enum.rb already. Before opening a PR, or continuing with the tests, I wanted to check here first, as the contributing guide says.
- Is there appetite for this at all, or would you prefer to keep instance_methods: strictly boolean and push people toward instance_methods: false + hand-rolled methods for finer control?
- Naming: I went with “predicate” (matches the “predicate methods” wording already used in the enum docs) and “bang” (the common Ruby term for ! methods). I am open to alternatives if there’s a preferred convention.
Many thanks!