如何在Rails应用程序中向我的CSV.generate端点添加自定义列? [英] How do I add a custom column to my CSV.generate endpoint in Rails app?

查看:111
本文介绍了如何在Rails应用程序中向我的CSV.generate端点添加自定义列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了此示例,但是我遇到了问题,每行显示nil值(在在原始值和我要添加的附加值之间.

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.

这是我的控制器端点:

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

这是我的模特:

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

当我下载CSV文件并打开它时,我看到一个空白值,然后是我在每行中附加的自定义值.如果我阅读了下载的CSV文件,这是我在前几行中看到的内容:

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"]]

请注意,每行(不在标题中)都有一个nil值.如果我排除自定义标头名称,然后像在那些nil值上方(在打开文件时为空白)上方那样附加值,则不再存在.

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.

因此,将出现自定义标头,为每行创建一个nil值.我该如何摆脱呢?

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

推荐答案

我从未发现为什么在每行中都包含这些nil值,但我想出了如何限制它们显示的方法.这是我在模型中修改函数的方式:

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!

这篇关于如何在Rails应用程序中向我的CSV.generate端点添加自定义列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆