Rails 迁移中的活动记录序列化
Rails active record serialisation inside migration
我正在进行迁移,我正在将保存为 ruby 哈希的数据从一个 table 移动到另一个。该列保存为 :text,在模型中序列化定义为哈希。
我想将此列移动到另一个 table 但如果我删除指定序列化类型的行(我想这样做是因为该字段将不再存在),那么迁移将处理数据作为一个字符串。是否可以定义必须如何在迁移文件本身中序列化数据?这样以后从头开始的迁移就不会在此时中断。
是的,只需在迁移文件中定义所有需要的信息:
# db/migrate/20190219114703_move_data_to_another_table.rb
class MyModel < ApplicationRecord
# here you need only the line that specifies the serialisation type
end
class MoveDataToAnotherTable < ActiveRecord::Migration[5.1]
def change
# here goes the migration itself
end
end
我正在进行迁移,我正在将保存为 ruby 哈希的数据从一个 table 移动到另一个。该列保存为 :text,在模型中序列化定义为哈希。
我想将此列移动到另一个 table 但如果我删除指定序列化类型的行(我想这样做是因为该字段将不再存在),那么迁移将处理数据作为一个字符串。是否可以定义必须如何在迁移文件本身中序列化数据?这样以后从头开始的迁移就不会在此时中断。
是的,只需在迁移文件中定义所有需要的信息:
# db/migrate/20190219114703_move_data_to_another_table.rb
class MyModel < ApplicationRecord
# here you need only the line that specifies the serialisation type
end
class MoveDataToAnotherTable < ActiveRecord::Migration[5.1]
def change
# here goes the migration itself
end
end