Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 序列化的属性没有作为正确的对象返回_Ruby_Ruby On Rails 3_Serialization_Deserialization - Fatal编程技术网

Ruby 序列化的属性没有作为正确的对象返回

Ruby 序列化的属性没有作为正确的对象返回,ruby,ruby-on-rails-3,serialization,deserialization,Ruby,Ruby On Rails 3,Serialization,Deserialization,我的模型serialize中有一个序列化属性:顶点,数组。在使用它时,一切似乎都正常,但在重新加载控制台或web请求后,序列化的数组将作为字符串返回,这显然不是我所期望的。以下是rails控制台中的相同过程: ruby-1.9.2-p290 :009 > g.vertices = [Vertex.new, Vertex.new] => [#<Vertex:0x000000036315e8 @neighbours={}, @options={}, @name=nil, @rea

我的模型serialize中有一个序列化属性:顶点,数组。在使用它时,一切似乎都正常,但在重新加载控制台或web请求后,序列化的数组将作为字符串返回,这显然不是我所期望的。以下是rails控制台中的相同过程:

ruby-1.9.2-p290 :009 > g.vertices = [Vertex.new, Vertex.new]
 => [#<Vertex:0x000000036315e8 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>, #<Vertex:0x00000003631458 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>] 
ruby-1.9.2-p290 :010 > g.inspect
 => "#<Graph id: 17, vertices: [#<Vertex:0x000000036315e8 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>, #<Vertex:0x00000003631458 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>], oriented: true, max_index: 0, trigger_limit: nil, created_at: \"2011-11-01 09:13:40\", updated_at: \"2011-11-01 09:17:30\", name: nil>" 
ruby-1.9.2-p290 :011 > g.save
   (0.4ms)  UPDATE "graphs" SET "vertices" = '---
- !ruby/object:Vertex
 neighbours: {}
 options: {}
 name: !!null 
 real_name: !!null 
 teacher: !!null 
 discipline: !!null 
 student_group: !!null 
- !ruby/object:Vertex
 neighbours: {}
 options: {}
 name: !!null 
 real_name: !!null 
 teacher: !!null 
 discipline: !!null 
 student_group: !!null 
', "updated_at" = '2011-11-01 09:21:11.516199' WHERE "graphs"."id" = 17
 => true 
ruby-1.9.2-p290 :012 > g.inspect
 => "#<Graph id: 17, vertices: [#<Vertex:0x000000036315e8 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>, #<Vertex:0x00000003631458 @neighbours={}, @options={}, @name=nil, @real_name=nil, @teacher=nil, @discipline=nil, @student_group=nil>], oriented: true, max_index: 0, trigger_limit: nil, created_at: \"2011-11-01 09:13:40\", updated_at: \"2011-11-01 09:21:11\", name: nil>" 
ruby-1.9.2-p290 :013 > reload!
Reloading...
 => true 
ruby-1.9.2-p290 :014 > g = Graph.last
  Graph Load (0.1ms)  SELECT "graphs".* FROM "graphs" ORDER BY "graphs"."id" DESC LIMIT 1
 => #<Graph id: 17, vertices: "---\n- !ruby/object:Vertex\n  neighbours: {}\n  option...", oriented: true, max_index: 0, trigger_limit: nil, created_at: "2011-11-01 09:13:40", updated_at: "2011-11-01 09:21:11", name: nil> 
ruby-1.9.2-p290 :015 > g.vertices.class
 => String 
ruby-1.9.2-p290 :016 > 

我猜它没有正确地反序列化可能是因为我在数组中的自定义类?任何提示都将不胜感激。谢谢。

保存序列化的Ruby实例并不十分优雅。我建议使用一种更传统的方法,例如composed_of,或任何其他实际保存参数以将实例构建到DB中而不是实例本身的解决方案:


是的,这绝对不是最漂亮的解决方案,但我不想创建数千个顶点对象并将它们放入数据库,如果它只用于一些计算。我仍然在想,为什么它不能工作?它的经典对象序列化如果我保存字符串、哈希或其他一些通用结构,它会工作吗?