Ignore rows with blank values while importing a CSV in rails application

Viewed 13940

I have an app where I allow users to import CSV data.

Everything was working until users started importing data with blank rows in the CSV file. I am using the following to grab the rows out of the CSV file:

CSV.readlines(import_file, headers: true, skip_blanks: true)

I thought that if I added the option to skip_blanks that it would do that but it hasn't. Any ideas on how I can ignore the blank rows.

Thanks!

5 Answers

If you also want to filter out columns that include just whitespaces use:

row.all? { |column| column.nil? || column.strip.empty? }
Related