在迁移中添加行

时间:2009-01-06 02:51:22

标签: ruby-on-rails database migration

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

class CreateProductCategories < ActiveRecord::Migration

  #defines the AR class
  class ProductType < ActiveRecord::Base; end

  def self.up

    #CREATE THE TABLES...

    load_data
  end
  def self.load_data
    #Use AR object to create default data
    ProductType.create(:name => "type")
  end
end

这很干净但是由于某些原因,不适用于持续版本的rails ......

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

谢谢!

5 个答案:

答案 0 :(得分:38)

用于迁移的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上测试过,但这也适用于许多早期版本。

答案 1 :(得分:9)

你可以使用灯具。这意味着在某个地方有一个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

答案 2 :(得分:7)

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

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

并且在运行之后:

rake db:seed

答案 3 :(得分:3)

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

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

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

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

对于那些将要执行迁移并维护产品的人来说,最简单的事情是什么。

答案 4 :(得分:1)

你真的不应该使用

ProductType.create

在您的迁移中。

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

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

例如,运行SQL要好得多:

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