【发布时间】:2011-11-01 09:33:55
【问题描述】:
我的模型serialize :vertices, Array 中有一个序列化属性。使用它时似乎一切正常,但是在我重新加载控制台(或在 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 ruby-on-rails-3 serialization deserialization