whizcreed
whizcreed

Reputation: 2772

Is there a good way to reduce value of an attribute via ActiveRecord in Rail4

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

Answers (1)

Hugo
Hugo

Reputation: 2129

Sale.where({}).decrement(:outstanding_balance, by = payment.amount)

Source: "decrement" method - RoR v4 API docs

Upvotes: 1

Related Questions