Rails 中是否有任何方法可以创建迁移的基本结构?

Is there any method in Rails that creates basic structure for migration?

例如我有以下代码:

create_table "users", force: :cascade do |t|
  t.string  "name"
end

我不想自己附加字符串,而是想像这样调用一些方法来为其构建基本的迁移框架:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table "users", force: :cascade do |t|
      t.string  "name"
    end
  end
end

在 Rails 中,您可以使用可用的生成器来定义大量样板代码,包括迁移。

要创建(大部分)示例,您可以使用此命令:

bin/rails generate migration CreateUsers name:string

这将生成以下迁移:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table "users" do |t|
      t.string  "name"
    end
  end
end

Rails guide on Active Record migrations对此进行了更详细的描述。请阅读本指南和其他一些指南,了解 rails 环境的基本用法。