This project is archived and is in readonly mode.

#5614 ✓invalid
ronin-22245 (at lighthouseapp)

'increment' and 'decrement' for ActiveRecord::Relation

Reported by ronin-22245 (at lighthouseapp) | September 11th, 2010 @ 08:38 AM | in 3.1

Hi guys,

I've added 2 new methods to ActiveRecord::Relation

http://github.com/pch/rails/commit/b9bcb52966addb714f5d634fe78f0899...
http://github.com/pch/rails/commit/486b583cc753fc53dc9a979f3d55c58b...

Essentially, this allows for executing queries like:

UPDATE some_table SET comment_count = comment_count + 1 WHERE id = 6;
Article.create(:title => "Hello", :counter => 0)
Article.increment(1, :counter)

ActiveRecord::Persistence#increment and #decrement need to fetch values from the database before updating them. That way, in some cases, there's a possibility of desynchronizing field value by updating it after it had been already overwritten in another request:

# counter == 0
# request 1:
article = Article.find(1)

# request 2:
article = Article.find(2)

#request 1:
article.increment(:counter)
article.save # counter == 1

#request 2:
article.increment(:counter)
article.save # counter == 1

Let me know what you think about it.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>

Attachments

Pages