Ruby on rails 在rails迁移中加载数据?
我有一个大型数据库,我需要在最初运行应用程序之前填充数据。最好在外部rake任务或内部迁移中执行此操作 真诚地,Ruby on rails 在rails迁移中加载数据?,ruby-on-rails,migration,Ruby On Rails,Migration,我有一个大型数据库,我需要在最初运行应用程序之前填充数据。最好在外部rake任务或内部迁移中执行此操作 真诚地, Tyler文件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
Tyler文件
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
将种子数据填充到数据库中。我认为这个文件只存在于Rails 3之后。但是,如果您想更早地使用它,只制作文件并自己添加rake任务应该不会有问题<代码>[~/code/rails]rails--版本rails 2.3.5[~/code/rails]rails种子创建。。。创建db/seeds.rb