Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the down migration for adding a polymorphic reference

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
like image 253
Damian Avatar asked Oct 02 '09 22:10

Damian


4 Answers

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!

like image 150
rbxbx Avatar answered Nov 05 '22 01:11

rbxbx


class RemoveAddressableFromAddresses < ActiveRecord::Migration
  def change
    remove_reference :addresses, :addressable, polymorphic: true, index: true
  end
end
like image 44
Judd Avatar answered Nov 05 '22 01:11

Judd


def self.down
  change_table :addresses do |t|
    t.remove_references :addressable, :polymorphic => true
  end
end
like image 7
Eben Geer Avatar answered Nov 05 '22 00:11

Eben Geer


What's the problem?

def self.down
  remove_column :addresses, :addressable_type
  remove_column :addresses, :addressable_id
end
like image 2
Leonid Shevtsov Avatar answered Nov 05 '22 00:11

Leonid Shevtsov