在 Rails 3 中播种数千条记录

发布于 2024-10-21 12:53:50 字数 155 浏览 2 评论 0原文

当我将项目转移到生产环境时,我有几个需要填充的表,每个表都有几千行。我现在已将数据存储在 CSV 文件中,但使用 Seed.rb 文件似乎很麻烦,因为 CSV 文件中的数据必须进行格式化才能满足 Seed.rb 格式。如果这只是少数行,那么就不会有这样的问题。加载此数据的最佳/最简单方法是什么?

I have several tables that need to be populated when I move my project to production, each of these tables has several thousand rows. I have the data stored in a CSV file now, but using the seed.rb file seems like it would be cumbersome because the data from my CSV file would have to be formatted to meet the seed.rb format. If this were only a handful of rows, it wouldn't such a problem. What would be the best/easiest way to get this data loaded?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

拧巴小姐 2024-10-28 12:53:50

我可能会使用一些自定义脚本和 faster_csv gem ,它具有快速解析 .csv 文件的好工具。然后您可以将字段映射到模型属性。

我将通过 TDD 作为模型方法来实现它,并使用 ActiveRecords 的 create 方法来实例化实例。虽然这比直接编写 SQL 慢,但更安全,因为您的数据将通过所有模型验证,并且您对数据完整性更有信心。

预先清除遗留数据导入中的数据完整性问题将为您省去很多麻烦。

I would probably use a little custom script and the faster_csv gem which has good tools to parse .csv files quickly. Then you can map the fields to model attributes.

I would implement this via TDD as model methods and use ActiveRecords's create method to instantiate instances. While this is slower than writing SQL straight out, it's safer in that your data will run through all the model validations and you have a better confidence in the data integrity.

Flushing out data integrity issues from legacy data import up front will save you a lot of trouble later.

阳光下慵懒的猫 2024-10-28 12:53:50

如果我使用 MySQL 执行此操作,我会使用 MySQL 的加载数据功能,例如

将文件“/my/rails/project/data.csv”中的加载数据替换为以“field_terminator”终止的表 table_name 字段以“line_terminator”终止的行;

如果表的设计不经常更改,您可以将这样的语句放入 perl、ruby 或 shell 脚本中。

If I were doing this using MySQL, I'd use MySQL's load data function, like

load data infile '/my/rails/project/data.csv' replace into table table_name fields terminated by 'field_terminator' lines terminated by 'line_terminator';

If the tables' designs do not change frequently, you could put a such a statement into a perl, ruby, or shell script.

塔塔猫 2024-10-28 12:53:50

就像其他人提到的那样,许多数据库都有批量加载支持。但如果您正在寻找 Rails 风格的解决方案; ar-extensions 具有批量插入

您还可以查看 ActiveWarehouse

  • < a href="https://github.com/zdennis/ar-extensions" rel="nofollow">https://github.com/zdennis/ar-extensions

Like others have mentioned many db's have bulk load support. But if your looking for a Rails style solution; ar-extensions has bulk insert

You also can checkout ActiveWarehouse

一向肩并 2024-10-28 12:53:50

fast_seeder gem 会帮助你。它使用多个插入从 CSV 文件填充数据库,并支持不同的数据库适配器

fast_seeder gem will help you. It populates database from CSV files using multiple inserts and supports different DB adapters

暮凉 2024-10-28 12:53:50

由于大多数答案都已过时,此宝石将为您提供帮助: https://github.com/zdennis/ activerecord-导入

例如,如果您有一本书籍集,您可以使用:

Book.import(books)

它只会执行一个 SQL 语句。

该宝石还与协会合作。

As most of the answers are outdated, this gem will help you: https://github.com/zdennis/activerecord-import.

E.g. if you have a collection of books, you can use:

Book.import(books)

It will execute only one SQL statement.

The gem also works with associations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文