Ruby on rails 如何为Rails中模型的哈希属性创建客户序列化程序?
我有以下几点Ruby on rails 如何为Rails中模型的哈希属性创建客户序列化程序?,ruby-on-rails,ruby,activerecord,serialization,ruby-on-rails-5,Ruby On Rails,Ruby,Activerecord,Serialization,Ruby On Rails 5,我有以下几点 class User < ApplicationRecord class JSONEncrypted def load(dbtext) JSON.load dbtext.decrypt end def dump(hash) (JSON.dump hash).encrypt end end store :profile, accessors: [:dob], coder: JSONEncrypted #商店:
class User < ApplicationRecord
class JSONEncrypted
def load(dbtext)
JSON.load dbtext.decrypt
end
def dump(hash)
(JSON.dump hash).encrypt
end
end
store :profile, accessors: [:dob], coder: JSONEncrypted
#商店:
客户序列化:我使用
attribute :profile, :encrypted
store :profile, accessors: [:dob], coder: JSON
使用我已根据此答案设置的EncryptedType:
但是,如果您仍然需要自定义数据类型序列化程序,我还发现。基本上,您必须返回一个与传递给coder:
的类完全相同的实例。我最初试图将JSON子类化,但发现它是一个模块,所以不知道如何将模块子类化
attribute :profile, :encrypted
store :profile, accessors: [:dob], coder: JSON