我正在尝试使用Store
将自定义Cluster类对象的数组存储为属性现在的问题是,当JSON编码器传递array
个对象时,如:
[
4.25, 136.25: 4 points, quant: 285.06307738701975,
47.5, 905.0: 2 points, quant: 11.180339887498949
]
这存储为:
[{"center"=>{"coords"=>{"x"=>5.5, "y"=>207.5}},
"points"=>
[{"coords"=>{"x"=>6.0, "y"=>195.0}, "z"=>8},
{"coords"=>{"x"=>5.0, "y"=>220.0}, "z"=>9}],
"moved"=>false},
{"center"=>{"coords"=>{"x"=>3.0, "y"=>65.0}},
"points"=>
[{"coords"=>{"x"=>4.0, "y"=>70.0}, "z"=>12},
{"coords"=>{"x"=>2.0, "y"=>60.0}, "z"=>13}],
"moved"=>false},
{"center"=>{"coords"=>{"x"=>47.5, "y"=>905.0}},
"points"=>
[{"coords"=>{"x"=>50.0, "y"=>900.0}, "z"=>10},
{"coords"=>{"x"=>45.0, "y"=>910.0}, "z"=>11}],
"moved"=>false}]
因此,对于每个数组元素,我得到ActiveSupport::HashWithIndifferentAccess
而不是Cluster类。如果我不使用json,那就完全失败了。 Ruby无法从属性4.25, 136.25: 4 points, quant: 285.06307738701975,
读取hash
作为对象。
我是否有其他选项可以将非标准对象存储为属性中的数组?
答案 0 :(得分:0)
为此获得了另一个解决方案 - 我没有使用带有json的ActiveRecord :: Store,而是使用了serialize方法:
serialize :cluster_array, Array
所以现在我可以将Cluster对象数组保存为属性。