EnvelopesController#index中的ArgumentError

时间:2019-12-13 20:54:43

标签: ruby ruby-on-rails-5

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

  

您需要至少提供一个验证     验证: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

1 个答案:

答案 0 :(得分:3)

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

validate :total_amount_in_all_envelope_can_not_be_greater_than_account_total