Answer the question
In order to leave comments, you need to log in
How to recurse?
Let's say there are categories and nested subcategories.
Here is my recursion code (Ruby):
def recurs(category_id)
@list << category_id
Category.where(parent_id: category_id).each do |category|
recurs(category.id)
end
end
@list = []
recurs(7)
@list = recurs(7)
Answer the question
In order to leave comments, you need to log in
Late but...
def recurs(category_id, list=[])
list << category_id
Category.where(parent_id: category_id).each do |category|
recurs(category.id, list)
end
list
end
recurs 7
More humane like this
def recurs(category_id)
list << category_id
Category.where(parent_id: category_id).each do |category|
recurs(category.id)
end
list
end
@list = recurs(7)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question