Using a Range in a Numericality Validation
Rails 7.0
allows a more compact way to specify allowed values in a numericality validation.
Instead of using combinations of greater_than
, greater_than_or_equal_to
, less_than
, and less_than_or_equal_to
,
you can use in
with a range.
Before
validates :setting_reuse, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 12 }
After
validates :setting_reuse, numericality: { in: 0..12 }
You get all the expressiveness that a range provides. Here are more examples each showing before and after:
validates :setting_reuse, numericality: { greater_than_or_equal_to: 2 }
validates :setting_reuse, numericality: { in: 2... }
validates :setting_reuse, numericality: { less_than: 12 }
validates :setting_reuse, numericality: { in: ...12 }
Written on June 19, 2024 by alistairmckinnell