Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 仅在更新时降低佣金值_Ruby On Rails_Ruby_Validation - Fatal编程技术网

Ruby on rails 仅在更新时降低佣金值

Ruby on rails 仅在更新时降低佣金值,ruby-on-rails,ruby,validation,Ruby On Rails,Ruby,Validation,目前,我有一个出价模型,其中包括一个百分比佣金属性。创建出价后,用户只能在更新时减少其固定佣金。实现这一目标的最佳方式是什么?如果属性增加或减少,“我的当前代码”将显示错误 validate :check_if_commission_percentage_increased, on: :update def check_if_commission_percentage_increased errors.add :base, 'You can only decrease your perce

目前,我有一个出价模型,其中包括一个百分比佣金属性。创建出价后,用户只能在更新时减少其固定佣金。实现这一目标的最佳方式是什么?如果属性增加或减少,“我的当前代码”将显示错误

validate :check_if_commission_percentage_increased, on: :update

def check_if_commission_percentage_increased
  errors.add :base, 'You can only decrease your percentage commission' unless self.commission_percentage > self.commission_percentage
end

*\u was
方法来自
ActiveModel::Dirty
。可以找到文档。

感谢您的精彩回答和指向文档的链接,我不知道该方法非常方便。没问题。链接是Dnuez24的贡献,尽管这要感谢他。
validate :method_name, on: :update

def method_name
  if commission_percentage > commission_percentage_was
    errors.add(:base, "error_message")
  end
end