Chr*_*ris 3 ruby migration activerecord ruby-on-rails dry
我有一个表,文件夹,我想要一些其他表引用,到目前为止,我的迁移脚本如下所示:
create_table :folders do |t|
t.timestamps
end
....
change_table table1 do |t|
t.references :folders
end
change_table table2 do |t|
t.references :folders
end
change_table table3 do |t|
t.references :folders
end
change_table table4 do |t|
t.references :folders
end
Run Code Online (Sandbox Code Playgroud)
由于我对每个表基本上做同样的事情,是否有更简洁和可维护的方式来编写它?
谢谢
尝试这样做:
[table1, table2, table3, table4].each do |tbl|
change_table tbl { |t| t.references :folders }
end
Run Code Online (Sandbox Code Playgroud)
我希望你不要用模式命名表,table{#n}并在实际代码中给它们好名字:)
| 归档时间: |
|
| 查看次数: |
165 次 |
| 最近记录: |