file = CSV.generate do |writer|
  array_of_hashes.each_with_index do |row, index|
    writer << row.keys if index == 0
    writer << row.values
  end
end