【问题标题】:Is there any method in Rails that creates basic structure for migration?Rails 中是否有任何方法可以为迁移创建基本结构?
【发布时间】:2016-05-30 14:53:59
【问题描述】:

例如我有以下代码:

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

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4 migration


    【解决方案1】:

    在 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 环境的基本用法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-06
      • 2022-01-24
      • 2021-06-29
      • 1970-01-01
      • 2022-11-26
      • 2018-02-22
      • 2022-01-10
      相关资源
      最近更新 更多