I’m using closure_tree for my Category model to structure the parent child relationships.
closure_tree provides a method to get the hierarchy as nested hashes:
How do I create hierarchical options for select form?
I started with this def on my Category model but it doesn’t seem to work and also I’m not sure if this is a right approach.
def self.nested_option(node_or_leaf = null, result = null)
result ||= []
node_or_leaf ||= Category.hash_tree
node_or_leaf.each do |k,v|
puts k.name
if (v.leaf?)
separator = ''
node_or_leaf.depth.times { separator = separator + '-' }
result << [k.name, [separator + node_or_leaf.name, node_or_leaf.id]]
else
nested_option(v, result)
end
end
result
end