In a Rails 5 validation, I'm trying to ensure that an inputted float have at most 3 digits after the decimal. Partly following this thread I have
validates :modifier, presence: true, format: { with: /\A\d+(?:\.\d{0,3})?\z/ }, numericality: { less_than: 100_000}
But the following test fails (using the shoulda matchers gem):
should_not allow_value(12345.1234).for(:modifier)
Error message: Expected errors when modifier is set to 12345.1234, got no errors
Even though the following tests succeed:
should allow_value(12345.123).for(:modifier)
should allow_value(12345).for(:modifier)
should_not allow_value(123456).for(:modifier)
Probably it has something to do with the regex only working for a string, whereas the :modifier
attribute I'm working with is coming in as a float. So I need to find a way to run the validation on a stringified version of the float...