What is the corresponding rollback?
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
精彩评论