ruby - EnvelopesController#index 中的参数错误
问题描述
我正在尝试验证帐户中的金额不能小于信封中的金额之和,但我收到此错误
您需要提供至少一项验证验证:total_amount_in_all_envelope_can_not_be_greater_than_account_total
这是我的模型
class Envelope < ApplicationRecord
belongs_to :account
validates :tag, presence: true
validates :account_id, presence: true
validates :amount, presence: true, numericality: {greater_than: 0, message: " must be greater than $0.0 "}
validates :total_amount_in_all_envelope_can_not_be_greater_than_account_total
def total_amount_in_all_envelope_can_not_be_greater_than_account_total
if
@account.envelopes.sum(&:amount) > @account.amount
errors.add(:amount, "Envelope amount cannot be reater than Account total" )
end
end
end
解决方案
使用自己的方法填充记录errors
时,调用的方法是validate
,而不是validates
。
validate :total_amount_in_all_envelope_can_not_be_greater_than_account_total
推荐阅读
- julia - 在 Julia 中删除结构
- javascript - 依赖于从服务器返回的值的组件的提供程序
- laravel-nova - Laravel Nova 减少 Select 选项或注入子句进行查询
- python - 相同的 python 脚本可以在一台计算机上运行,但不能在另一台计算机上运行
- c - ncurses 无法在结构的 xy 位置显示元素
- csv - LOAD DATA INFILE 始终跳过第一行,但未设置为 IGNORE
- html - 单击按钮后如何向下滚动 50px?
- c++ - 如何对二维栅格中的二维点进行排序
- php - 以小时为单位找出 2 次之间的差异:分钟
- go - 如何重用 POST 请求并覆盖其有效负载?