How do I update a cumulative field in a Rails database (using ActiveRecord or Mongoid)?

Posted by picardo on Stack Overflow See other posts from Stack Overflow or by picardo
Published on 2010-12-30T07:50:30Z Indexed on 2010/12/30 7:54 UTC
Read the original article Hit count: 233

I want to update a field in a database table that has to have a cumulative value. So basically I need to find the current value of the field and update it using a new number.

My first inefficient try at this (in Mongoid) is:

v = Landlord.where(:name=>"Lorem")
v.update_attributes(:violations=>v.violations + 10)

Is there a simple method than making one query to read, then sum up, and another query to write?

© Stack Overflow or respective owner

Related posts about activerecord

Related posts about ruby-on-rails3