deruse
deruse

Reputation: 2881

ruby on rails concurrent adds

I have a model with a has_many association. The association also has an :after_add component which increments a counter. I was wondering how this would play out in a concurrent scenario (e.g. when two different instances are added to the has_many at the exact same time). Would the counter increase by 2? Does rails internally get a lock, increment, and release the lock? Is the add and :after_add callback seen as one atomic action?

Thanks.

Upvotes: 1

Views: 1412

Answers (1)

coreyward
coreyward

Reputation: 80041

As long as you're using the increment methods and not doing a SELECT, increment in Ruby, then UPDATE, you should be fine. More info: rails 3 & activerecord: do I need to take special record locking precautions to update a counter field?

Upvotes: 2

Related Questions