我有一个Products表,想要添加一个列:
t.references :imageable, :polymorphic => true
我尝试通过执行以下操作来生成迁移:
$ rails generate migration AddImageableToProducts imageable:references:polymorphic
但很明显我做错了。有没有人能提点建议?谢谢
当我尝试在生成迁移后手动添加它时,我是这样做的:
class AddImageableToProducts < ActiveRecord::Migration
def self.up
add_column :products, :imageable, :references, :polymorphic => true
end
def self.down
remove_column :products, :imageable
end
end
但它仍然没有起作用
发布于 2013-03-04 23:02:13
你正在尝试做的事情还没有在rails的稳定版本中实现,所以目前米歇尔的答案是正确的。但是这个特性将在rails 4中实现,并且已经在edge版本中可用,如下所示(根据这个CHANGELOG):
$ rails generate migration AddImageableToProducts imageable:references{polymorphic}
发布于 2014-11-25 05:38:28
您还可以执行以下操作:
class AddImageableToProducts < ActiveRecord::Migration
def change
add_reference :products, :imageable, polymorphic: true, index: true
end
end
发布于 2017-09-13 15:17:40
你可以试试rails generate migration AddImageableToProducts imageable:references{polymorphic}
https://stackoverflow.com/questions/5534579
复制相似问题