Rails 迁移:加载默认数据
加载种子数据的最佳方式? 我有一个与用户表紧密耦合的作者表。 我还进行了迁移来更改这两个表。 我想添加默认管理员用户,但我想确保在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
尝试使用 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
你不能创建单独的迁移并单独运行它们吗
cant you just create seperate migrations and run them seperatly
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).
您的选择:
就我个人而言,我使用修改后的yaml-db。 我喜欢将所有种子数据构建到我的开发环境中,并将其备份在我的存储库中,当我上线时,我可以使用 rake 任务加载它。
Your options:
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.