在 Rails 中播种数据库的最佳方法是什么? [英] What is the best way to seed a database in Rails?

查看:33
本文介绍了在 Rails 中播种数据库的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rake 任务,用于在我的 Rails 应用程序中填充一些初始数据.例如,国家/地区、州、移动运营商等

I have a rake task that populates some initial data in my rails app. For example, countries, states, mobile carriers, etc.

我现在设置的方式是,我在/db/fixtures 中的文件中有一堆 create 语句和一个处理它们的 rake 任务.例如,我拥有的一个模型是主题.我在/db/fixtures 中有一个 theme.rb 文件,如下所示:

The way I have it set up now, is I have a bunch of create statements in files in /db/fixtures and a rake task that processes them. For example, one model I have is themes. I have a theme.rb file in /db/fixtures that looks like this:

Theme.delete_all
Theme.create(:id => 1, :name=>'Lite', :background_color=>'0xC7FFD5', :title_text_color=>'0x222222',
                      :component_theme_color=>'0x001277', :carrier_select_color=>'0x7683FF', :label_text_color=>'0x000000',
                      :join_upper_gradient=>'0x6FAEFF', :join_lower_gradient=>'0x000000', :join_text_color=>'0xFFFFFF',
                      :cancel_link_color=>'0x001277', :border_color=>'0x888888', :carrier_text_color=>'0x000000', :public => true)

Theme.create(:id => 2, :name=>'Metallic', :background_color=>'0x000000', :title_text_color=>'0x7299FF',
                      :component_theme_color=>'0xDBF2FF', :carrier_select_color=>'0x000000', :label_text_color=>'0xDBF2FF',
                      :join_upper_gradient=>'0x2B25FF', :join_lower_gradient=>'0xBEFFAC', :join_text_color=>'0x000000',
                      :cancel_link_color=>'0xFF7C12', :border_color=>'0x000000', :carrier_text_color=>'0x000000', :public => true)

Theme.create(:id => 3, :name=>'Blues', :background_color=>'0x0060EC', :title_text_color=>'0x000374',
                      :component_theme_color=>'0x000374', :carrier_select_color=>'0x4357FF', :label_text_color=>'0x000000',
                      :join_upper_gradient=>'0x4357FF', :join_lower_gradient=>'0xffffff', :join_text_color=>'0x000000',
                      :cancel_link_color=>'0xffffff', :border_color=>'0x666666', :carrier_text_color=>'0x000000', :public => true)
puts "Success: Theme data loaded"

这里的想法是我想为用户安装一些常用主题.我对这个方法有问题.

The idea here is that I want to install some stock themes for users to start with. I have a problem with this method.

设置 ID 不起作用.这意味着,如果我决定添加一个主题,让我们称其为红色",那么我只想将主题语句添加到这个夹具文件中,并调用 rake 任务来重新设定数据库的种子.如果我这样做,因为主题属于其他对象,并且在重新初始化时它们的 id 会发生变化,所有链接都将断开.

Setting the ID does not work. This means that if I decide to add a theme, let's call it 'Red', then I would simply like to add the theme statement to this fixture file and call the rake task to reseed the database. If I do that, because themes belong to other objects and their id's change upon this re-initialization, all links are broken.

我的问题首先是,这是处理数据库播种的好方法吗?在之前的帖子中,这是向我推荐的.

My question is first of all, is this a good way to handle seeding a database? In a previous post, this was recommended to me.

如果是这样,我如何对 ID 进行硬编码,这有什么缺点吗?

If so, how can I hard code the IDs, and are there any downsides to that?

如果没有,为数据库设置种子的最佳方法是什么?

If not, what is the best way to seed the database?

我将非常感谢包含最佳实践的长期且深思熟虑的答案.

I will truly appreciate long and thought out answers that incorporate best practices.

推荐答案

更新,因为这些答案有点过时(尽管有些仍然适用).

Updating since these answers are slightly outdated (although some still apply).

在 rails 2.3.4、db/seeds.rb 中添加的简单功能

Simple feature added in rails 2.3.4, db/seeds.rb

提供一个新的rake任务

Provides a new rake task

rake db:seed

适合填充常见的静态记录,如州、国家等...

Good for populating common static records like states, countries, etc...

http://railscasts.com/episodes/179-seed-data

*请注意,如果您已经创建了设备,则可以使用它们来填充 db:seed 任务,方法是将以下内容放入您的 seed.rb 文件(来自 railscast 剧集):

*Note that you can use fixtures if you had already created them to also populate with the db:seed task by putting the following in your seeds.rb file (from the railscast episode):

require 'active_record/fixtures'
Fixtures.create_fixtures("#{Rails.root}/test/fixtures", "operating_systems")

对于 Rails 3.x,使用 'ActiveRecord::Fixtures' 而不是 'Fixtures' 常量

For Rails 3.x use 'ActiveRecord::Fixtures' instead of 'Fixtures' constant

require 'active_record/fixtures'
ActiveRecord::Fixtures.create_fixtures("#{Rails.root}/test/fixtures", "fixtures_file_name")

这篇关于在 Rails 中播种数据库的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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