2010-05-14 127 views

回答

1

文件db/seeds.rb是专为这一目的。它应该在您创建Rails项目时自动生成。它的顶部有解释如何使用它的注释:

# This file should contain all the record creation needed to seed the database with its default values. 
# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). 
# 
# Examples: 
# 
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }]) 
# Major.create(:name => 'Daley', :city => cities.first) 

你已经填充了之后,用rake db:seed与种子数据来填充数据库。

+0

我认为这个文件只存在于Rails 3以上。但是如果你想早些使用它,那么只需制作文件并自己添加rake任务就不会有问题。 – Matchu 2010-05-14 21:53:55

+0

不正确。 '[〜/ Code/rails] rails --version Rails 2.3.5 [〜/ Code/rails] rails seeds create ... create db/seeds.rb' – 2010-05-14 21:57:33