假设你开始你的模型Thing
与未加密属性note
。
1)添加迁移添加一个字段encrypted_note
和填充它
class EncryptThing < ActiveRecord::Migration
def up
rename_column :things, :note, :old_note
add_column :things, :encrypted_note, :string
# if you want to use per-attribute iv and salt:
# add_column :things, :encrypted_note_iv, :string
# add_column :things, :encrypted_note_salt, :string
Thing.find_each do |t|
t.note = t.old_note
t.save
end
remove_column :things, :old_note
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
2)行添加到您的模型,以指定的加密属性:
attr_encrypted :note, :key => Rails.application.config.key
# if you want to use per-attribute iv and salt, add this to the line above:
# , :mode => :per_attribute_iv_and_salt
3)运行迁移
rake db:migrate
read_attribute很好,谢谢!...我试着在与encrypted_note同时获得popul ated清除数据库中的“note”字段做user.write_attribute('note',''),但我得到这个错误:私人方法'write_attribute'调用...任何想法? – Alpha 2011-06-15 07:22:21