Reputation: 2772
I have a column called outstanding balance. And when I get a payment as success I need to reduce the outstanding balance. I could do it explicitly like:
Sale.where({}).update_attribute(:outstanding_balance, self.outstanding_balance - payment.amount)
#Rather there should be way like:
Sale.where({}).reduce(:outstanding_balance, payment.amount)
Just wondering what should be the best way to do this?
Upvotes: 0
Views: 399
Reputation: 2129
Sale.where({}).decrement(:outstanding_balance, by = payment.amount)
Source: "decrement" method - RoR v4 API docs
Upvotes: 1