将嵌套实体导出到CSV文件 [英] Export nested entities to CSV file

查看:220
本文介绍了将嵌套实体导出到CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些嵌套数据的rails应用程序,我想导出为CSV文件

I have a rails app with some nested data that I'd like to export as a CSV file

这些模型看起来像:

class ContainerRecord < ActiveRecord::Base
  has_many      :child_records 

class ChildRecord < ActiveRecord::Base
  belongs_to :container_record



我想要能够导出CSV文件,其中每个ContainerRecord在一行中,其信息在前几列,并且其余列中每个ChildRecord的值。

I'd like to be able to export a CSV file with each ContainerRecord on a row with its information in the first few columns and a value from each ChildRecord in the remaining columns.

我不能保证ChildRecords与每个ContainerRecord相关联,我不在乎我是否为每一行有不同数量的非空列。

I can't guarantee the number of ChildRecords associated with each ContainerRecord and I don't care if I have a different number of non-null columns for each row.

我试图使用FasterCSV,但是我得到所有的孩子记录的数据推送到一列,而不是每列的列。

I've attempted to use FasterCSV, but I get all of the data for the child records shoved into one column rather than a column for each.

这是我可以用FasterCSV吗?

Is this something that I can do with FasterCSV? If not, what method can I use to accomplish my goal?

推荐答案

我最终找到了一个不错的 csv_builder上的教程,让我完全按照我的想法做最少的努力,并让我更贴近一点MVC架构。

I ended up finding a nice tutorial on csv_builder that let me do exactly what I wanted to do with minimal effort and allowed me to stick a little more closely to the MVC architecture.

这篇关于将嵌套实体导出到CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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