EnvelopesController#index中的ArgumentError

问题描述 投票:0回答:1

我正在尝试验证帐户中的金额不能小于信封中金额的总和但我收到此错误

您需要提供至少一个验证验证:total_amount_in_all_envelope_can_not_be_greater_than_account_total enter image description here

这是我的模特

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
ruby ruby-on-rails-5
1个回答
2
投票

[当使用您自己的方法来填充记录的errors时,调用的方法是validate,而不是validates

validate :total_amount_in_all_envelope_can_not_be_greater_than_account_total
© www.soinside.com 2019 - 2024. All rights reserved.