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_ad开发者_JAVA技巧d callback seen as one atomic action?
Thanks.
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?
精彩评论