Ruby on Rails - Increment and decrement

by Jess Brown

If you're using rails and have an integer column in database that you want to increment or decrement, rails provides a nice abstraction for you to use. Normally you'd have do something like this:

def increase_credits
  update_attributes(:credits => credits + 1)

Then you can call it like:


With the increment and decrement you can do it this way:



@user.decrement(:credits, 2)  # decrease credits by 2

It just saves setting up the method and allows you to pass an integer to increment by!

Decrement:! Increment:

comments powered by Disqus