I tried to write validation for Rails to ensure that the price entered on the form is greater than zero. It works ... sort of. The problem is that when I run it, val turns into an integer, so it thinks .99 is less than .1. What happens and how do I fix the code?
class Product < ActiveRecord::Base protected def self.validates_greater_than_zero(*attr_names) validates_each(attr_names) do |record, attr, val| record.errors.add(attr, "should be at least 0.01 (current val = #{val.to_f})") if val.nil? || val < 0.01 end end public validates_presence_of :title, :description, :image_url validates_numericality_of :price validates_greater_than_zero :price end
user297008
source share