2010-05-14 125 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