在Rails中种子数据库的最好方法是什么? [英] What is the best way to seed a database in Rails?
问题描述
我有一个rake任务填充我的rails应用程序中的一些初始数据。例如,国家,州,移动运营商等。
I have a rake task that populates some initial data in my rails app. For example, countries, states, mobile carriers, etc.
现在我设置的方式是,我有一堆create语句在/ db /夹具和处理它们的耙子任务。例如,我有一个模型是主题。我在/ 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
b $ b
适用于填充常见的静态记录,例如州,国家/地区等...
Good for populating common static records like states, countries, etc...
http://railscasts.com/episodes/179-seed-data
*请注意你可以使用fixtures,如果你已经创建它们也填充db:种子任务通过把以下在您的seeds.rb文件(从轨道剧集):
*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屋!