我正在尝试验证帐户中的金额不能少于信封中金额的总和 但我收到此错误
您需要至少提供一个验证 验证: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
答案 0 :(得分:3)
使用自己的方法填充记录的errors
时,调用的方法是validate
,而不是validates
。
validate :total_amount_in_all_envelope_can_not_be_greater_than_account_total