Rails 3:如何validationA <B,其中A和B都是模型属性?
我想validationcustomer_price >= my_price
。 我尝试了以下方法:
class Product my_price ... end
( customer_price
是数据库中Products
表中的一列,而my_price
则不是。)
结果如下:
NameError in ProductsController#index undefined local variable or method `my_price' for #
在Rails 3中执行此操作的正确方法是什么?
创建自定义validation器:
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
您需要进行特定validation:
validate :more_than_my_price def more_than_my_price if self.customer_price >= self.my_price errors.add(:customer_price, "Can't be more than my price") end end