【发布时间】:2015-04-03 15:37:38
【问题描述】:
在我的项目中,其中一项功能是将数据导出为 CSV。
我在模型中编写了一个函数来执行此操作,效果很好,但如果我想将关联的模型值导出到同一个 CSV 文件中怎么办?
我尝试了多种方法,但都没有成功。
产品.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 中导出“产品其他信息”模型值,我该怎么办?
【问题讨论】:
标签: ruby-on-rails ruby csv