You need to take a look at your real problem again. A feature of Rails is that the string is automatically converted automatically either to the corresponding decimal value, or to 0.0 otherwise.
What's happening
1) You can store anything in the ActiveRecord field. Then it is converted to the appropriate type for the database.
>> product.price = "a" => "a" >> product.price => #<BigDecimal:b63f3188,'0.0',4(4)> >> product.price.to_s => "0.0"
2) You must use the correct check to make sure that only reliable data is saved. Is there something wrong with keeping the value 0? If not, then you do not need verification.
3) You do not need to verify that the number will be stored in the database. Since you declared the db field to be a decimal field, it will ONLY contain decimal numbers (or null if you give the field null values).
4) Your check was oriented to the check line. Thus, the validation regular expression changed 0.0 BigDecimal to "0.0" and it passed validation. Why do you think your check has been ruled out?
5) Why are you worried that other programmers store strings in your price field?
Are you trying to avoid the erroneous price at zero price? There are several ways around this. You can check the value as it arrives (before it is converted to decimal) to see if its format is correct. See AR Section “Overwriting Default Accessories”
But I think it would be dirty and error prone. You will need to set an error record from the Setter object or use a flag. And a simple class check will not work, remember that form data is always included in the string.
Recommended . Instead, ask the user to confirm that they are designed to set the price to 0 for the product using the optional AR-only field (a field that is not stored in dbms).
For instance,
attr_accessor :confirm_zero_price
Notes The above thing, which is VERY important for inclusion in your tests.
Also I have had similar situations in the past. As a result of my experience, I now record in the database the name of the person who said that the value should really be $ 0 (or negative), and let them have a 255 char reason field to justify them. Saves a lot of time later when people are wondering what is the reason.