I'm trying to make a nested (and thus sorted) array of hashes like so:
[{ stack: 'stack name', id: 1,
boxes: [{
box: 'whatever box',
id: 1,
vars: [{
var: 'some name',
id: 22,
},
{ var: 'another name',
id: 4
}]
}, {
box: 'another box',
id: 99,
vars: [{
var: 'another',
id: 999
}]
}
}]
}]
The method I've come up with so far is this which doesn't work but I'm totally stack as to how to nest these objects to maintain their hierarchy (a Stack can have many Boxes, a Box can have many TemplateVariables.
master = []
@template.stacks.alphabetised.each_with_index do |stack, i|
master << { stack: stack.name, id: stack.id }
stack.boxes.indexed.each_with_index do |box, j|
master[i] = { box: box.name, id: box.id }
box.template_variables.indexed.each do |var|
master[i][j] = { var: var.name, id: var.id }
end
end
end
master
This seems to return nothing despite those objects definitely being there (and I know my structure is off, too). Am I doing something wrong?