Blog posts with refactoring tag
Three ways of iterating over tree-like ActiveRecord structures
You are building an e-commerce app and at some point you introduce a Category model as a part of Product data. A Category can have various subcategories, which are represented by the same model. At this point, it's obvious we are dealing with a tree-like data structure. Step by step, I'll introduce three ways of iteration over such a structure and compare their performance.