Apologies if there's a better way for asking this.
I'm trying to export User data to a .csv file but I'm having problems iterating through arrays. I'm getting the contents of the whole array in one column and nothing in the following columns. Example:
Suppose I have the headers Company and Country:
<% headers = [
... # some other headers
"Company",
"Country"
] %>
<%= CSV.generate_line(headers, row_sep:"", col_sep: ",") %>
Then I have an array of strings with the user values for Company name and Country, and I have to fill the corresponding column in the file with the correct value.
<% @user_preferences = ["Microsoft", "USA"] %>
I'm doing something like
<%= CSV.generate_line([
... # some other fields
@user_preferences.each do |value|
value
end
], row_sep:"", col_sep: ",").html_safe %>
But instead of getting each value in a different column, I get ["Microsoft", "USA"] in first column (Company) and nothing in the Country column.
What I understand is happening is it's filling the first column with whatever @user_preferences returns before going into the iteration block instead of assigning each value to a separate column.
I've tried with a hash instead of an array, but get the same results when iterating through it's keys/values (I get both values in the same column).
Is there a way to escape the iteration and fill each column with the correct value?