Rails 迁移:加载默认数据

发布于 2024-08-01 17:53:37 字数 157 浏览 2 评论 0原文

加载种子数据的最佳方式? 我有一个与用户表紧密耦合的作者表。 我还进行了迁移来更改这两个表。 我想添加默认管理员用户,但我想确保在 CreateDefaultAdmin(或其他)迁移运行之前,已创建两个表并且已为这些表运行所有迁移。 有这样做的最佳实践吗? 抱歉,如果已经有人问过这个问题。

Best way to load seed data? I have an Author table that is tightly coupled with a Users table. I also have migrations to alter both of these tables. I want to add a default admin user but I want to make sure that both tables are created and all migrations have run for these tables before my CreateDefaultAdmin (or whatever) migration runs. Is there a best practices for doing this? Sorry if this was already asked.

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

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

发布评论

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

评论(4

狼性发作 2024-08-08 17:53:37

尝试使用 Factory Girl 作为紧密耦合模型的固定装置替代品:

http://www.thoughtbot.com/projects/工厂女孩

Try Factory Girl as a fixtures replacement for tightly coupled models:

http://www.thoughtbot.com/projects/factory_girl

伴随着你 2024-08-08 17:53:37

你不能创建单独的迁移并单独运行它们吗

cant you just create seperate migrations and run them seperatly

森林很绿却致人迷途 2024-08-08 17:53:37

Rails 的最新(边缘?)版本包含一个 db/seeds.rb 文件,您可以在其中放置代码以在数据库中创建记录。 在那之前,迁移是可行的方法,但是当架构不断变化时,它们可能会变得混乱(这是迁移文件的全部要点)。

The latest (edge?) version of Rails includes a db/seeds.rb file into which you can place code to create records in your database. Until then migrations are the way to go, but they can be messy when the schema keeps changing (which is the entire point of migration files).

高冷爸爸 2024-08-08 17:53:37

您的选择:

  1. 使用此处概述的迁移:http://railspikes.com/2008/ 2/1/loading-seed-data
  2. 使用第 3 方插件,例如: seed -fudb-populate
  3. 使用固定装置,详见铁轨钉文章。
  4. 等待 Rails 3 并使用新的 seeds.rb: http://github.com/rails/rails /commit/4932f7b38f72104819022abca0c952ba6f9888cb

就我个人而言,我使用修改后的yaml-db。 我喜欢将所有种子数据构建到我的开发环境中,并将其备份在我的存储库中,当我上线时,我可以使用 rake 任务加载它。

Your options:

  1. Use migrations as outlined here: http://railspikes.com/2008/2/1/loading-seed-data
  2. Use a 3rd party addon like: seed-fu or db-populate
  3. Use fixtures, which is outlined in the rails spike article.
  4. Wait for rails 3 and use the new seeds.rb: http://github.com/rails/rails/commit/4932f7b38f72104819022abca0c952ba6f9888cb

Personally I use a modified yaml-db. I like to build up all my seed data into my dev environment, and keep it backed up in my repository, when I go live I can load it with a rake task.

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