在迁移中添加行

我想知道哪些是在Rails迁移中向数据库表添加记录的首选方法。 我读过Ola Bini的书(Jruby on Rails),他做了类似这样的事情:

class CreateProductCategories < ActiveRecord::Migration #defines the AR class class ProductType  "type") end end 

这很干净但是由于某种原因,它不适用于持续版本的导轨……

问题是,如何使用默认数据(如用户或其他东西)填充数据库?

谢谢!

你可以使用灯具。 这意味着在某个地方有一个yaml文件,其中包含您要插入的数据。

这是我在我的一个应用程序中为此提交的变更集:

db/migrate/004_load_profiles.rb

 require 'active_record/fixtures' class LoadProfiles < ActiveRecord::Migration def self.up down() directory = File.join(File.dirname(__FILE__), "init_data") Fixtures.create_fixtures(directory, "profiles") end def self.down Profile.delete_all end end 

db/migrate/init_data/profiles.yaml

 admin: name: Admin value: 1 normal: name: Normal user value: 2 

用于迁移的Rails API文档显示了一种更简单的方法来实现此目的。

http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

 class CreateProductCategories < ActiveRecord::Migration def self.up create_table "product_categories" do |t| t.string name # etc. end # Now populate the category list with default data ProductCategory.create :name => 'Books', ... ProductCategory.create :name => 'Games', ... # Etc. # The "down" method takes care of the data because it # drops the whole table. end def self.down drop_table "product_categories" end end 

在Rails 2.3.0上测试过,但这也适用于许多早期版本。

您也可以在seeds.rb文件中定义,例如:

 Grid.create :ref_code => 'one' , :name => 'Grade Única' 

然后运行:

 rake db:seed 

您的迁移可以访问所有模型,因此您不应在迁移中创建类。

我正在使用最新的rails,我可以确认您发布的示例肯定是OUGHT工作。

然而,迁移是一种特殊的野兽。 只要你清楚,我没有看到ActiveRecord::Base.connection.execute("INSERT INTO product_types (name) VALUES ('type1'), ('type2')")

这样做的好处是,您可以使用某种GUI或Web前端轻松生成它,以填充您的起始数据,然后执行mysqldump -uroot database_name.product_types

无论是什么使得那些将要执行迁移和维护产品的人最简单。

你真的不应该使用

 ProductType.create 

在您的迁移中。

我做过类似的事,但从长远来看,他们不能保证工作。

运行迁移时,您使用的模型类是运行迁移时的模型类,而不是创建迁移时的模型类。 您必须确保永远不会以阻止迁移运行的方式更改模型。

例如,运行SQL要好得多:

 [{name: 'Type', ..}, .. ].each do |type| execute("INSERT INTO product_types (name) VALUES ('#{type[:name]} .. ) end