我正在将一堆记录导出为CSV文件,像这样:
def self.to_csv
attributes = %w(full_name first_name last_name created_at status)
CSV.generate(headers: true) do |csv|
csv << attributes
all.each do |lead|
csv << attributes.map { |attr| lead.send(attr) }
end
end
end
但我需要标题读作Full Name
而不是full_name
。我需要一个哈希来匹配名称,这很好,但我如何在CSV.generate
中将新的标题名称写入CSV文件中?
更新查找哈希表。
def lookup
{
full_name: 'Full Name', first_name: 'First Name', last_name: 'Last Name', created_at: 'Generation Date', status: 'Status'
}
end
attributes
数组。 - t56kheaders: true
对输出结果没有任何影响。生成参数似乎是无用的。 - Adeynack