开发者

What is the corresponding rollback?

开发者 https://www.devze.com 2023-03-03 16:55 出处:网络
I have the following ActiveRecord migration: class CreateSubjects < ActiveRecord::Migration def self.up

I have the following ActiveRecord migration:

class CreateSubjects < ActiveRecord::Migration
  def self.up
    create_table :subjects do |t|
      t.string :title
      t.timestamps
    end

    change_table :projects do |t|
      t.refer开发者_开发百科ences :subjects
    end
  end

  def self.down
    drop_table :subjects
    remove_column :projects, :subjects_id #defeats the purpose of having references
  end
end

I actually like the references style. Unfortunately I could not find the rollback equivalent of references in the self.down section. If I write remove_column :projects, :subjects_id I can as well write t.integer :subjects_id, which would make it safer.


It is called remove_references.

t.remove_references :subjects

Be careful! Rails uses singular by convention, should be:

def self.up
  create_table :subjects do |t|
    t.string :title
    t.timestamps
  end

  change_table :projects do |t|
    t.references :subject
  end
end

def self.down
  drop_table :subjects
  change_table :projects do |t|
    t.remove_references :subject
  end
end
0

精彩评论

暂无评论...
验证码 换一张
取 消