1

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?

2
  • 2
    "I'm trying to export User data to a .csv file" – but your code looks like you're trying to write the CSV output to a HTML template. Commented Nov 17, 2021 at 13:29
  • Yes, the .csv is just like any other html.erb view. But instead of rendering it, a download prompt is given. Commented Nov 17, 2021 at 13:41

1 Answer 1

1

It looks like you're trying to splice the contents of an array into another array literal. You can use the splat operator for this:

<%= CSV.generate_line([:some, :other, :fields, *@user_preferences],
                      row_sep:"", col_sep: ",").html_safe %>

It is equivalent to this:

[:some, :other, :fields, *["Microsoft", "USA"]]

Which gives:

[:some, :other, :fields, "Microsoft", "USA"]
Sign up to request clarification or add additional context in comments.

4 Comments

The first CSV.generate_line creates the first line of headers. The second one generates each line containing the User data under each header. The problem I'm having when iterating through the array is that instead of having each value of the array under the corresponding header, I get all the values under the first header and nothing in the following ones.
@pinkfloyd90 did you try the suggested fix?
@Stefan I opted for building the row contents array outside the CSV.generate_line and then passing the whole array to it.
@pinkfloyd90 the problem was that you added the @user_preferences array to the row-array instead of adding its elements. (which is what the splat operator * achieves) Hope you got it sorted out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.