我想验证一下customer_price >= my_price
。我尝试了以下方法:
class Product < ActiveRecord::Base
attr_accessor :my_price
validates_numericality_of :customer_price, :greater_than_or_equal_to => my_price
...
end
(customer_price
是其中的一列Products
数据库中的表,同时my_price
isn't.)
结果如下:
NameError in ProductsController#index
undefined local variable or method `my_price' for #<Class:0x313b648>
在 Rails 3 中执行此操作的正确方法是什么?
创建自定义验证器:
validate :price_is_less_than_total
# other model methods
private
def price_is_less_than_total
errors.add(:price, "should be less than total") if price > total
end
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)