简体   繁体   English

如何在 Rails 应用程序中向我的 CSV.generate 端点添加自定义列?

[英]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.我遵循了这个例子,但我遇到了每行中显示nil值的问题(在原始值和我添加的附加值之间。

Here's my controller endpoint:这是我的 controller 端点:

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:这是我的 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.当我下载 CSV 文件并打开它时,我看到一个空白值,然后是我在每一行中附加的自定义值。 If I read the downloaded CSV file, here's what I see in the first few rows:如果我阅读下载的 CSV 文件,以下是我在前几行中看到的内容:

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).请注意,每一行都有一个nil值(不在标题中)。 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.如果我排除我的自定义 header 名称,然后排除 append 值,就像我所做的那样,那些nil值(打开文件时为空白)不再存在。

So, it appears the custom header creating a nil value for each row.因此,似乎自定义 header 为每一行创建了一个nil值。 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.我从来没有发现为什么每行都包含这些nil值,但我想出了如何限制它们出现。 Here's how I modified the function within my model:以下是我在 model 中修改 function 的方法:

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!希望这对将来的人有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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