I'm quite new to Rails and I've been attempting to flatten out an array of JSON objects fetched from the DB in my controller to assemble a flattened data structure to pass into my cross filter (i'm making some dc.js visualizations). I've managed to get an nested array of objects like so:
['class': 'biology', 'category': 'science', 'students': [{'name': 'sarah', 'year':freshman}, {'name':'bob', 'year': 'sophomore'}] ]
This is the structure that I'm aiming to achieve:
[ {'name':'sarah', 'year':'freshman', 'class': 'biology', 'category': 'science'}, {'name':'bob', 'year': 'sophomore', 'class': 'biology', 'category': 'science'}]
I've managed to build an array of these JSON objects in my controller with the class to students association through my controller, but I've totally hit a wall on how to properly flatten out the dataset as an array of UN-nested objects. I'd essentially like to flatten out an object for each student and have that as one object represented in the new array. Any pointers would be much appreciated.
So far, I've come across utilizing:
JSON.parse(objects).each do |obj|
#flatten json here
end
Is it just a matter of iterating through the students attribute, and for each student create a new object with all the associated attributes? Any pointers in the right direction would be much appreciated.