Rails迁移:如何将属性从模型移动到其他模型

时间:2019-02-06 14:49:53

标签: ruby-on-rails ruby migration ruby-on-rails-5

我有一个具有step_id属性的Report模型,并且我想将此属性移到ReportSheets模型,有没有一种方法可以从终端生成迁移,或者我应该自己编写代码:

class CreateReports < ActiveRecord::Migration[4.2]
  def change
    create_table :reports, id: :uuid do |t|
      t.uuid     :declarant_id

      t.integer  :reference
      t.datetime :sent_at
      t.uuid     :step_id

      t.boolean  :is_archived, default: false
      t.uuid     :updated_by_id
      t.string   :device_id

      t.timestamps null: false
    end

    add_index :reports, :reference, unique: true

    add_index :reports, :declarant_id
    add_index :reports, :device_id
    add_index :reports, :step_id

  end
end


class CreateReportSheets < ActiveRecord::Migration[4.2]
  def change
    create_table :report_sheets, id: :uuid do |t|
      t.string  :title,        null: false
      t.boolean :is_template,  default: false
      t.uuid    :report_id
      t.integer :seed_number
      t.integer :order
      t.hstore  :steps, array: true, default: '{}', null: false

      t.uuid  :template_id

      t.boolean :is_archived,   default: false
      t.uuid    :updated_by_id
      t.string  :device_id

      t.timestamps null: false
    end

    add_index :report_sheets, :report_id
    add_index :report_sheets, :template_id
    add_index :report_sheets, :seed_number
    add_index :report_sheets, :is_template

    add_index :report_sheets, :is_archived
    add_index :report_sheets, :device_id
  end
end

,并且在两个模型之间存在某种关系,例如add_index:reports,:step_id?

0 个答案:

没有答案