Encrypting database columns with Rails 7
In Rails 7, Active Record includes the option to encrypt database columns. To start run bin/rails db:encryption:init
, then copy the resulting keys to your app's credentials.yml
. Now in your model, you can tell Active Record to encrypt a column by using the encrypts
which takes a db column name as an argument. For example:
class User < ApplicationRecord
encrypts :super_secret_data
end
Active record will automatically decrypt the data upon retrieval. See more here.
Tweet