0

如何更新以前使用 gem attr_encrypted 未加密的现有记录。

我目前有text一个名为的表中的列,AppointmentNote它只是一个字符串。我现在想要一个名为note加密的列(使用 attr_encrypted)。

我已经添加了列

encrypted_note encrypted_note_iv

当我AppointmentNote.create(note: "blah")正确加密时,这很好用,并且对该记录的任何进一步更新都很好用。

问题在于迁移之前创建的记录。如何将列中的所有数据迁移text到新的加密列encrypted_noteencrypted_note_iv

这是模型

class AppointmentNote < ApplicationRecord
  attr_encrypted_options.merge!(encode: true)
  attr_encrypted :note, key: SOME_KEY
  ...
end

如果我做我认为明显的解决方案就是简单地回滚 AppointmentNote.first.update(note: "rawr")

谢谢

4

1 回答 1

0

您应该能够通过保存来更新它们。就像是:

rails g migration update_apartment_notes_note_for_encryption

打开该生成的文件。

def change
  AppointmentNote.find_each do |apartment_note|
    apartment_note.save 
  end
end

rake db:migrate

注意:使用 find_each 比all拥有大量记录更明智。这里的目标是迭代它们。

于 2018-08-28T02:35:30.483 回答