首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何生成迁移以使引用多态

如何生成迁移以使引用多态
EN

Stack Overflow用户
提问于 2011-04-04 12:31:34
回答 3查看 74.9K关注 0票数 139

我有一个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

但它仍然没有起作用

EN

回答 3

Stack Overflow用户

发布于 2013-03-04 23:02:13

你正在尝试做的事情还没有在rails的稳定版本中实现,所以目前米歇尔的答案是正确的。但是这个特性将在rails 4中实现,并且已经在edge版本中可用,如下所示(根据这个CHANGELOG):

$ rails generate migration AddImageableToProducts imageable:references{polymorphic}
票数 301
EN

Stack Overflow用户

发布于 2014-11-25 05:38:28

您还可以执行以下操作:

class AddImageableToProducts < ActiveRecord::Migration
  def change
    add_reference :products, :imageable, polymorphic: true, index: true
  end
end
票数 47
EN

Stack Overflow用户

发布于 2017-09-13 15:17:40

你可以试试rails generate migration AddImageableToProducts imageable:references{polymorphic}

票数 18
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5534579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档