[英]Updating existing unencrypted records with attr_encrypted
How do I update existing records that were previously unencrypted with the gem attr_encrypted. 如何更新以前使用gem attr_encrypted解密的现有记录。
I currently have the column text
in a table called AppointmentNote
which is just a string. 我目前在名为
AppointmentNote
的表中具有列text
,它只是一个字符串。 I now want to have a column called note
which is encrypted (with attr_encrypted). 我现在想有一列称为
note
的列,该列已加密(使用attr_encrypted)。
I've added the columns 我已经添加了列
encrypted_note
encrypted_note_iv
encrypted_note
encrypted_note_iv
encrypted_note
encrypted_note_iv
This works well when I do AppointmentNote.create(note: "blah")
it encrypts properly, and any further updates on that record work well. 当我执行
AppointmentNote.create(note: "blah")
它可以正确加密,并且该记录上的任何进一步更新都可以正常工作。
The problem is with records created prior to the migration. 问题出在迁移之前创建的记录。 How do i migrate all the data from the column
text
into the new encrypted columns encrypted_note
and encrypted_note_iv
我如何将所有数据从列
text
迁移到新的加密列encrypted_note
和encrypted_note_iv
This is the model 这是模特
class AppointmentNote < ApplicationRecord
attr_encrypted_options.merge!(encode: true)
attr_encrypted :note, key: SOME_KEY
...
end
If i do the what I thought the obvious solution was it simply roll back AppointmentNote.first.update(note: "rawr")
如果我做了我认为显而易见的解决方案,则只需回滚
AppointmentNote.first.update(note: "rawr")
Thanks 谢谢
You should be able to just update them all with a save. 您应该可以保存所有更新。 Something like:
就像是:
rails g migration update_apartment_notes_note_for_encryption
Open that generated file. 打开该生成的文件。
def change
AppointmentNote.find_each do |apartment_note|
apartment_note.save
end
end
rake db:migrate
Note: Using find_each is wiser than all
if you have tons of records. 注意:如果您有大量记录,则使用find_each比
all
方法更明智。 The goal here is to iterate over them. 这里的目标是遍历它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.