Preventing Recursion in Ruby

Sometimes you need to prevent code from executing recursively. I had to do this recently in order to prevent an infinite recursion in ActiveRecord callbacks. A before-save hook needed to save a parent model attribute, which because of an :autosave option would then try to save the child model, which would then try to save the parent… and so on until the stack overflowed.

Read on at the CodeBenders blog…

1 comment

  1. I tried to use this code to prevent recursion with a validate method in a model. It didn’t prevent. Is there anything unique to the validate method that would prevent the prevent from working?

Leave a Reply

Your email address will not be published. Required fields are marked *