Skip to content

Require Object#in? in the numericality validator - #59899

Merged
fatkodima merged 1 commit into
rails:mainfrom
carlosdanielpohlod:numericality-in-require
Nov 20, 2026
Merged

fatkodima merged 1 commit into
rails:mainfrom
carlosdanielpohlod:numericality-in-require

Conversation

@carlosdanielpohlod

Copy link
Copy Markdown
Contributor

Motivation / Background

NumericalityValidator checks the :in option with in?, but numericality.rb doesn't require active_support/core_ext/object/inclusion. When only Active Model is loaded, validation raises:

require "active_model"

class Item
  include ActiveModel::Validations
  attr_accessor :quantity
  validates :quantity, numericality: { in: 1..3 }
end

item = Item.new
item.quantity = 2
item.valid?
# NoMethodError: undefined method 'in?' for an instance of Integer

Inside a Rails app or the Active Model test suite something else has already loaded in?, so it doesn't show up there.

Detail

Adds the missing require. The test runs the snippet above in a separate process so the rest of the suite can't hide it.

Additional information

#44467 rewrites this code path to use clusivity instead of in?. This is a one line fix for the current code and doesn't conflict with that direction.

Checklist

  • 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.

@carlosdanielpohlod
carlosdanielpohlod marked this pull request as ready for review November 20, 2026 18:18
NumericalityValidator checks the :in option with `in?`, which comes from
active_support/core_ext/object/inclusion, but the file never required it.
In an app that only loads Active Model, `numericality: { in: 1..3 }`
raised NoMethodError on validation.
@fatkodima
fatkodima force-pushed the numericality-in-require branch from 8091a42 to dca39e6 Compare November 20, 2026 22:14
@fatkodima
fatkodima merged commit 6104b12 into rails:main Nov 20, 2026
2 of 4 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