简体   繁体   中英

How do I add a custom column to my CSV.generate endpoint in Rails app?

I followed this example but I'm having issues with a nil value showing up in each row (in between the original values and the additional value I'm adding.

Here's my controller endpoint:

def daily_grocery_carts_overview_export
  @data = DailyRetailerShop.all.order("start_date ASC")
  respond_to do |format|
    format.html { redirect_to root_path }
    format.csv { send_data @data.to_csv, filename: "DailyGroceryCarts-#{Time.now.strftime("%Y%m%d%H%M%S")}.csv" }
  end
end

Here's my model:

class DailyRetailerShop < ActiveRecord::Base

  def self.to_csv
    # generate site abbreviations & name to add to CSV file
    site_abbreviations = {}
    Partner.all.each do |p|
      site_abbreviations[p[:site_abbreviation]] = p[:name]
    end

    CSV.generate do |csv|
      # remove certain columns
      export_columns = column_names - %w(id site_abbreviation created_at updated_at)
      # add custom column header
      headers = export_columns << 'Website'
      # add to csv file
      csv << headers
      all.each do |item|
        row = item.attributes.values_at(*export_columns).insert(-1, site_abbreviations[item.site_abbreviation])
        csv << row
      end
    end
  end

end

When I download the CSV file and open it up I see a blank value followed by the custom value I appended in each row. If I read the downloaded CSV file, here's what I see in the first few rows:

data = CSV.read("downloaded_file.csv")
puts data[0..3]
=> [["start_date", "grocery_retailer", "retailer_shops", "Website"], ["2019-10-15", "walmart", "25", nil, "Website1"], ["2019-10-15", "walmart", "24", nil, "Website2"], ["2019-10-15", "instacart", "23", nil, "Website3"]]

Notice there is a nil value for each row (not in the headers). If I exclude my custom header name and then append values as I did above those nil values (blanks when I open the file) are no longer there.

So, it appears the custom header creating a nil value for each row. How do I get rid of that?

I never found out why those nil values are being included in each row but I figured out how to restrict them from showing up. Here's how I modified the function within my model:

class DailyRetailerShop < ActiveRecord::Base

  def self.to_csv
    # generate site abbreviations & name to add to CSV file
    site_abbreviations = {}
    Partner.all.each do |p|
      site_abbreviations[p[:site_abbreviation]] = p[:name]
    end

    CSV.generate do |csv|
      # remove certain columns
      export_columns = column_names - %w(id site_abbreviation created_at updated_at)
      # add custom column header
      headers = export_columns << 'Website'
      # add to csv file
      csv << headers
      all.each do |item|
        # exclude the last item before inserting site name (custom header)
        row = item.attributes.values_at(*export_columns)[0..-2].insert(-1, site_abbreviations[item.site_abbreviation])
        csv << row
      end
    end
  end

end

Hope this helps someone in the future!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM