0

I have a model called CustomizedBooking where I need to validate paid amount value equal to the price column of the database. Following is code snippet

validate :paid_amount_and_price
def paid_amount_and_price_check
    if paid_amount == price column value of the database
    errors.add(:paid_amount, "Must be equal to the price to process further!")
end

Please! somebody, suggest me

2 Answers 2

1

We don't have to use any custom validation here, try this:

validates_numericality_of :paid_amount,
                          equal_to: ->(object) { object.price.to_f },
                          message: "Must be equal to the price to process further!"

or

validates :paid_amount, 
          :numericality => { equal_to: ->(object) { object.price.to_f },
                             message: "Must be equal to the price to process further!" }
Sign up to request clarification or add additional context in comments.

Comments

0

try below code in model:

validate :paid_amount_and_price
def paid_amount_and_price_check
    unless paid_amount.to_f == price.to_f
      errors.add(:paid_amount, "Must be equal to the price to process further!")
    end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.