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)
end

Then you can call it like:

@user.increase_credits

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

@user.increment!(:credits)

or

@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: http://apidock.com/rails/ActiveRecord/Base/decrement! Increment: http://apidock.com/rails/ActiveRecord/Base/increment%21


comments powered by Disqus