2
在我的项目中,其中一项功能是将数据导出为CSV。使用关联模型值在导轨中导出为CSV格式
我已经在模型中写了一个函数来做到这一点,它工作正常,但如果我想在相同的CSV文件中导出关联的模型值呢?
我尝试了多种方法,但没有成功。
Product.rb:
has_many :product_other_informations
def self.to_csv
CSV.generate do |csv|
csv << ["Name", "Description", "Tags", "Meta_keywords", "Page_title", "Meta_description", "Slug", "Published_status", "Other_information", "Other_information_value"]
all.each do |product|
csv << [product.name, product.description, product.tags, product.meta_keywords, product.page_title, product.meta_description, product.slug, product.is_published]
end
end
end
这是用来导出唯一的产品价值。如果我想将“产品其他信息”模型值导出为相同的CSV,我该怎么办?
,因为我有很多product_other_informations,这一切的名称是在单column.How出口我才能sepatare列的所有名称。 – 2015-04-04 08:53:47
然后遍历'product_other_informations'并将它们分开放在不同的列中! – 2015-04-04 16:14:51