Export as CSV accessing elements of an array and inserting line breaks

I have a document stored in Mongo inside the jobs collection that looks like this:

"products" : [  {   "id" : "51bc0cc39727911120000145",  "price" : 19,   "quantity" : 1,     "tax" : 0 },    {   "id" : "51bc0cc39727911120000171",  "price" : 14,   "quantity" : 1,     "tax" : 0 },    {   "id" : "51bc0cca9727911120000a23",  "price" : 308,  "quantity" : 1,     "tax" : 0.092500001192093 } ]

products is an array of arrays. When you export as CSV, this entire line prints out when you access the "products" key (naturally).

What I need to do:

Reach inside products and grab EACH of the elements, printing them in appropriate columns in the CSV AND inserting a line break in the CSV such that they line up under the appropriate columns.

Something like:

ID PRICE QUANTITY TAX ID PRICE QUANTITY TAX ID PRICE QUANTITY TAX

Line 1 above to the left has the actual JOB NUMBER, but lines 2 and 3 wouldn't have that (it would be redundant).

The crazy part is I need some each logic that will grab however many parts are added to the job (from none to infinity).

Does anyone have any idea how to pull this off?

I'm using the comma gem, so I have some code in my model:

products do |products| #I've tried untold combos here - none of which work end

Help!

UPDATE:

Something like this gets the first element out of products (and payments which is structured the same)

products do |products|
      products.each do |p|
        @pid = p["id"]
        @pid = Inventory.find_by_id(@pid)
        if @pid == nil
          @pid = "None"
        else
          @pid = @pid.parts_name
        end
      end
      @pid
    end
    products 'Price' do |products|
      products.each do |p|
        @price = p["price"]
      end
      @price
    end
    products 'Quantity' do |products|
      products.each do |p|
        @qty = p["quantity"]
      end
      @qty
    end
    products 'Tax' do |products|
      products.each do |p|
        @tax = (p["tax"].to_f * (p["price"].to_f * p["quantity"].to_i)).round(2)
      end
      @tax
    end

Any idea on for loop logic ?


ANSWERS:


maybe somethin like this would do

column_names = ["id","price","quantity","tax"] 
CSV.generate do |csv|
   csv << column_names
   products.each do |p|
     @pid = Inventory.find_by_id(p["id"])
     csv << [@pid ? @pid.parts_name : "none", p["price"], p["quantity"], (p["tax"].to_f * (p["price"].to_f * p["quantity"].to_i)).round(2) ]
   end
 end

Here is a snippit of the final solution:

products do |products|
      @prod=[]
      products.each do |p|
        @pid = p["id"]
        @pid = Inventory.find_by_id(@pid)
        if @pid == nil
          @prod << "None"
        else
          @prod << @pid.parts_name
        end
      end
      @prod.to_sentence(:words_connector=>"\r",:two_words_connector=>"\r",:last_word_connector=>"\r")
    end

The last part was one of those "screw it I'll try everything" approaches where I stumbled on an alternate use for the "to_sentence" method. It works...



 MORE:


 ? Export to a csv file
 ? error sas export to csv
 ? Powershell Export-Csv creates empty files
 ? Exporting Python List to CSV in "column" format
 ? Is providing a Promise as a module's export a valid pattern for asynch initialization in Node.js?
 ? Is providing a Promise as a module's export a valid pattern for asynch initialization in Node.js?
 ? Is providing a Promise as a module's export a valid pattern for asynch initialization in Node.js?
 ? Sync elasticsearch on connection with database - nodeJS
 ? Native Support for Promises in Node.js
 ? Node.js not change data first time using promise