rai*_*ner 116 ruby-on-rails polymorphic-associations rails-migrations
我有一个Products表,想要添加一列:
t.references :imageable, :polymorphic => true
Run Code Online (Sandbox Code Playgroud)
我试图通过以下方式为此生成迁移:
$ rails generate migration AddImageableToProducts imageable:references:polymorphic
Run Code Online (Sandbox Code Playgroud)
但我显然做错了.任何人都可以提出任何建议吗?谢谢
当我在生成迁移后尝试手动将其放入时,我这样做:
class AddImageableToProducts < ActiveRecord::Migration
def self.up
add_column :products, :imageable, :references, :polymorphic => true
end
def self.down
remove_column :products, :imageable
end
end
Run Code Online (Sandbox Code Playgroud)
它仍然没有奏效
小智 255
你想要做的还没有在稳定版本的rails中实现,所以Brandon的答案现在是正确的.但是这个功能将在rails 4中实现,并且已经在边缘版本中可用,如下所示(根据此CHANGELOG):
$ rails generate migration AddImageableToProducts imageable:references{polymorphic}
Run Code Online (Sandbox Code Playgroud)
Mic*_*ley 106
据我所知,没有用于多态关联的内置生成器.生成空白迁移,然后根据需要手动修改.
更新:您需要指定要更改的表.根据这个SO回答:
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
Run Code Online (Sandbox Code Playgroud)
fre*_*gel 33
您还可以执行以下操作:
class AddImageableToProducts < ActiveRecord::Migration
def change
add_reference :products, :imageable, polymorphic: true, index: true
end
end
Run Code Online (Sandbox Code Playgroud)
hut*_*usi 16
你可以试试 rails generate migration AddImageableToProducts imageable:references{polymorphic}
| 归档时间: |
|
| 查看次数: |
61982 次 |
| 最近记录: |