且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在迁移中添加行

更新时间:2023-12-04 08:37:22

您可以为此使用固定装置.这意味着在某个地方有一个 yaml 文件,其中包含您要插入的数据.

You could use fixtures for that. It means having a yaml file somewhere with the data you want to insert.

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

Here is a changeset I committed for this in one of my app:

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