假設你開始你的模型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