bgr*_*mes 2 activerecord ruby-on-rails
当模型使用单表继承(STI)存储在数据库中时,如何编写Rails迁移以重命名ActiveRecord子类?我唯一能想到的是使用原始sql update语句将类型列值的值从旧的子类型名称更改为新的子类型名称.
您可以使用execute在迁移中运行原始查询
class YourMigration < ActiveRecord::Migration
def up
execute "UPDATE table_name SET type = 'Namespace::NewSubclass' WHERE type = 'Namespace::OldSubclass'"
end
def down
execute "UPDATE table_name SET type = 'Namespace::OldSubclass' WHERE type = 'Namespace::NewSubclass'"
end
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
783 次 |
| 最近记录: |