9

I have the folowing migration but don't know what to use in the down method

change_table :addresses do |t|
  t.references :addressable, :polymorphic => true
end
JJD
  • 50,076
  • 60
  • 203
  • 339
Damian
  • 1,543
  • 15
  • 22

5 Answers5

16

actually,

   change_table :addresses do |t|
     t.remove_references :addressable
   end

would be a bit railsier, no?

edit: As Eben Geer points out

   change_table :addresses do |t|
     t.remove_references :addressable, :polymorphic => true
   end

is the correct way to do this. Cheers!

rbxbx
  • 314
  • 3
  • 8
  • 1
    this will cause the addressable_type column to be left behind. it should be t.remove_references :addressable, :polymorphic => true – Eben Geer Dec 14 '11 at 07:54
  • I would upvote this answer if it included @EbenGeer his comment – Arjan Feb 07 '13 at 12:37
10
class RemoveAddressableFromAddresses < ActiveRecord::Migration
  def change
    remove_reference :addresses, :addressable, polymorphic: true, index: true
  end
end
Judd
  • 116
  • 1
  • 5
  • Please take a minute to explain what your code does and how it answers this question. – Bond Sep 01 '15 at 00:20
  • This should now be the correct answer: remove_reference can nowadays (since Rails 4.2 at least) be rolled back. That means, Rails knows how to roll back a remove_reference by a add_reference. So this code can be rolled back (though data may by lost in the migration) – Martin M Nov 22 '19 at 08:23
7
def self.down
  change_table :addresses do |t|
    t.remove_references :addressable, :polymorphic => true
  end
end
Eben Geer
  • 3,696
  • 3
  • 31
  • 34
2

What's the problem?

def self.down
  remove_column :addresses, :addressable_type
  remove_column :addresses, :addressable_id
end
Leonid Shevtsov
  • 14,024
  • 9
  • 51
  • 82
-2

What's wrong with this?

def self.down
  remove_column :addresses, :addressable
end
Mark A. Nicolosi
  • 82,413
  • 11
  • 44
  • 46