user4778326
user4778326

Reputation:

BigDecimal validation raises an error on non numeric values

To validate the price we require, we use the following

  validates :price, numericality: {greater_than_or_equal_to: 0.01, less_than_or_equal_to: 2000}

It works fine until someone tries to enter a string (in this example "FREE"). This returns the error:

invalid value for BigDecimal(): "FREE"

Is there a clean way of validating this without returning a 500 error?

Upvotes: 2

Views: 979

Answers (1)

Abdul-Rahman Ahmad
Abdul-Rahman Ahmad

Reputation: 163

In case it might help someone who stumbled on such case, seems like upgrading the BigDecimal gem solves the issue

gem "bigdecimal", ">= 1.3.2"

Reference: https://jamescrisp.org/2017/09/26/bigdecimal-fix-for-rails-4-with-ruby-2-4/

Upvotes: 1

Related Questions