我有一个产品表并想要添加一列:
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
还是没有效果
最佳答案
您想要做的事情尚未在 Rails 的稳定版本中实现,因此 Michelle 的答案目前是正确的。但该功能将在 Rails 4 中实现,并且已经在 Edge 版本中提供,如下(根据此 CHANGELOG ):
$ rails generate migration AddImageableToProducts imageable:references{polymorphic}
某些 shell 可能需要使用 \
转义 {polymorphic}
:
$ rails generate migration AddImageableToProducts imageable:references\{polymorphic\}
关于ruby-on-rails - 如何生成迁移以使引用多态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5534579/