1
アクティブなレコードモデルに、テキストタイプのconfiguration
という列があります。その列がそうのように、カスタムクラスでシリアル化さ:Railsのシリアル化でハッシュが正しくダンプされない
class MyModel < ApplicationRecord
serialize :configuration, MySerializer
end
クラスMySerializer
は、以下のクラスのメソッドがあります。
def dump(configuration)
configuration.to_json if configuration
end
def load(configuration)
obj = new
obj.json_hash = JSON.parse(configuration) if configuration.present?
obj
end
これは、attrのアクセサjson_hash
とクラスMySerializer
のインスタンスをインスタンス化します。
MyModel.create(configuration: {"key" => 1})
をそして、私はMyModel.first
を行うと、私はこれを取得:
は今、ここでの問題はだ、私がやっている
...
configuration:
#<MySerializer:0x00000007faa558
@json_hash={"json_hash"=>{"key" => 1}
私のようなものになっ期待していた:
を@json_hash = {"key" => 1}
繰り返しキーを取得する理由は何ですか。attrアクセサ内の@json_hash
?
ありがとうございます!