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

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?

Answers


MongoDB provides special atomic update operation support for exactly this scenario -- take a look at the $inc modifier in the Updating documentation. Your example in the plain mongo console would be:

db.landlords.update( { name:"Lorem" }, { $inc: { violations : 10 } } );

With Mongoid this would be:

Landlord.where(:name => "Lorem").first.inc(:violations, 10)

This will set violations to 10 if the field is defined on your model but hasn't otherwise been initialized to any value yet.

But actually, this still executes two queries (look up the landlord, then update it). To do it all in one, you need to go through a Mongoid::Collection object, which proxies most calls straight through to the Mongo Ruby driver. That would look like this:

Landlord.collection.update({ :name => "Lorem" }, '$inc' => { :violations => 10 })

It helps to be familiar with the Ruby driver for cases like this when you want to take more direct control. Its API tries to match closely with the mongo Javascript shell syntax.


Need Your Help

Functional testing with Warden?

ruby-on-rails authentication devise warden

I'm trying to do functional testing and need to login with Warden.

Lotus Notes API giving error while accessing the NSF

java lotus-notes lotus-domino lotus

I have been working for accessing the Lotus Notes(.nsf) data from the external server using JAVA Lotus notes API, but I'm getting the following error for Lot of functions while accessing the NSF.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.