Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding value with existing value rails update

I am having a column price with value 10(may differ) and i need to add value 5(may differ) to the existing value 10 which is the good way to update this in rails.

Am using following rails query to find by customer_id

customer_id=25

refund_update = Refund.find_by customer_id: customer_id

like image 796
Prabhakaran Avatar asked Feb 21 '26 21:02

Prabhakaran


1 Answers

You can use several ways to update the value:

with validation:

refund_update.increment('price', 5)

or

refund_update.update_attributes({'price': refund_update.price+5})

without validation:

refund_update.increment!('price',5)

or

refund_update.update_attribute('price', refund_update.price+5)
like image 195
Bigxiang Avatar answered Feb 24 '26 12:02

Bigxiang



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!