Interestingly, ActiveRecord::CounterCache does this the appropriate way. https://github.com/rails/rails/blob/master/activerecord/lib/active_record/counter_cache.rb#L72
On Sunday, January 20, 2013 6:56:37 PM UTC-5, ajsharp wrote: > > The method is here: > https://github.com/rails/rails/blob/master/activerecord/lib/active_record/persistence.rb#L288 > . > > The method takes the in-memory attribute value and increments it by the > specified amount. A safer approach (from an isolation standpoint) would be > to let the database determine the value. Instead of telling the database > what value to persist in the database, the SQL can written (at least for > postgres) so that the database will atomically increment a value: > > UPDATE "posts" SET view_count = view_count + 1 WHERE id=123; > > Currently, rails generates the following SQL: > > UPDATE "posts" SET "view_count" = 3, "updated_at" = '2013-01-20 > 23:20:24.154852' WHERE "posts"."id" = 123 > > It would be great to see a method like this to perform atomic update > operations for databases that support it. If there's support for this, I'm > happy to write the patch. Thanks! > -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Core" group. To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-core/-/p2dAMnSuYIkJ. To post to this group, send email to rubyonrails-core@googlegroups.com. To unsubscribe from this group, send email to rubyonrails-core+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-core?hl=en.