how to generate migration to make references polymorphic
What you are trying to do is not yet implemented in the stable version of rails so Michelle's answer is the right one for now. But this feature will be implemented in rails 4 and is already available in the edge version as follows (according to this CHANGELOG):
$ rails generate migration AddImageableToProducts imageable:references{polymorphic}
Some shells may need {polymorphic}
escaped with a \
:
$ rails generate migration AddImageableToProducts imageable:references\{polymorphic\}
You could also do the following:
class AddImageableToProducts < ActiveRecord::Migration
def change
add_reference :products, :imageable, polymorphic: true, index: true
end
end
Before Rails 4 there was no built-in generator for polymorphic associations. If you are using an early version of Rails generate a blank migration and then modify it by hand according to your needs.
Update: You'll need to specify which table you're changing. According to this SO answer:
class AddImageableToProducts < ActiveRecord::Migration
def up
change_table :products do |t|
t.references :imageable, polymorphic: true
end
end
def down
change_table :products do |t|
t.remove_references :imageable, polymorphic: true
end
end
end
Rails 4 added a generator for polymorphic associations (see simon-olivier answer)