到目前为止,我有以下内容
class User < ApplicationRecord
def name=(s)
super s.encrypt
end
def name
(super || '').decrypt
end
我想替换它并执行类似
的操作@@encrypted_fields = [:name, :phone, :address1, ...]
def self.initialize
@@encrypted_fields.each do |s|
setter = (s.to_s+'=')(value) -> do
super value.encrypt
end
getter = (s.to_s) -> do
(super || '').decrypt
end
User.send(:include, setter)
User.send(:include, getter)
end
end
我不能对所有记录执行此操作,例如电子邮件,当然(他们将如何登录?),并且某些字段不够重要,不足以打扰。
我试过
@@encrypted_fields = [:name, :phone, :address1]
@@encrypted_fields.each do |m|
setter = (m.to_s+'=').to_sym
getter = m
define_method(setter) do |v|
super v.encrypt
end
define_method(getter) do
(super || '').decrypt
end
end
基于Use def_method inside ActiveRecord model
但它给了
不支持从define_method()定义的方法传递super的隐式参数。明确指定所有参数。
(super || '').decrypt