ruby on rails - Validation after database submission -
i have payments model follows:
class payment < activerecord::base attr_accessible :amount, :method, :payment_date, :reference_no, :invoice_id belongs_to :invoice validates :amount, presence: true validates :method, presence: true validates :payment_date, presence: true validate :payment_not_more_than_balance def payment_not_more_than_balance if amount > self.invoice.balance self.errors.add :amount, 'payments should less or equal invoice amount' end end end
i trying run validation whereby once attempts make payment greater invoice balance, validation error issued.
currently, code above makes submission database runs validation.
that if have invoice balance of 2000, when make payment of 2000, payment submitted (leaving me invoice balance of 0) , later issued error 'payments should less or equal invoice amount' not neccessary.
the error should run if attempt make payment of 2000 when invoice balance 0
how can correct that?
use before filter on validation
before_save :payment_not_more_than_balance
Comments
Post a Comment