【问题标题】:Rails active record serialisation inside migration迁移中的 Rails 活动记录序列化
【发布时间】:2019-02-19 12:43:30
【问题描述】:

我正在进行迁移,我正在将保存为 ruby​​ 哈希的数据从一个表移动到另一个表。该列保存为 :text,并且在模型中序列化定义为 Hash。

我想将此列移动到另一个表中,但如果我删除了指定序列化类型的行(我想要这样做,因为该字段将不再存在),那么迁移会将数据视为字符串。是否可以在迁移文件本身中定义数据必须如何序列化?这样将来从头开始的迁移不会在此时中断。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord rails-activerecord


    【解决方案1】:

    是的,只需在迁移文件中定义所有需要的信息:

    # 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
    

    【讨论】:

    • 哦,太棒了,没有意识到你可以这样做,将验证它是否有效并给出一个绿色的勾:)
    • @Makoto,很高兴为您提供帮助!
    猜你喜欢
    • 2015-10-06
    • 2013-07-27
    • 2012-04-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-30
    相关资源
    最近更新 更多