mercredi 3 octobre 2018

Ruby before_validation triggers infinite loop of call back

Product Model has an attribute_1. If recalculation is required for attribute_1 then before_validation will call. It giving SystemStackError: stack level too deep because self.save! triggers the before_validation. How to stop infinite loop of call back.

before_validation :method_1, :if => :recalculation_required_attribute

I am using optimistic locking using lock_version. 'update_all' will not increase the lock_version. So I am using save!. It is calling the infinite look of call back.

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

Aucun commentaire:

Enregistrer un commentaire