Tag: race condition

Ruby:如何使用update_all(属性)处理乐观锁定

我正在尝试为种族条件实施乐观锁定。 为此,我在Product:Model中通过迁移添加了额外的列lock_version 。 #Product: Model’s new field: # attribute_1 # lock_version :integer(4) default(0), not null before_validation :method_1, :if => :recalculation_required_attribute def method_1 #### #### if self.lock_version == Product.find(self.id).lock_version Product.where(:id => self.id).update_all(attributes) self.attributes = attributes self.save! end end 产品型号具有attribute_1 。 如果attribute_1需要重新计算,则before_validation: method_1将调用。 我使用lock_version使用乐观锁定。 但是, update_all不会增加lock_version 。 所以我开始使用save! 。 现在我收到一个新错误: SystemStackError: stack level too deep因为self.save! 触发before_validation: method1 […]