Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何为Rails中模型的哈希属性创建客户序列化程序?_Ruby On Rails_Ruby_Activerecord_Serialization_Ruby On Rails 5 - Fatal编程技术网

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