Here is an example. I looked for this reason because I was too lazy to think about it, but since I found it here, I decided to do it right. Here is the code you can use with the standard way to create Rails associations.
def all_children(children_array = []) children_array += self.children children.each do |child| return child.all_children(children_array) end return children_array end
Note the two uses there of return . If you missed the inside, you will find yourself with only one level of deep tree.
Hooray!
source share