【问题标题】:Serialized attributes not coming back as proper objects序列化属性没有作为正确的对象返回
【发布时间】: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


    【解决方案1】:

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

    http://api.rubyonrails.org/classes/ActiveRecord/Aggregations/ClassMethods.html

    【讨论】:

    • 是的,它绝对不是最漂亮的解决方案,但如果它只用于一些计算,我不想创建数千个顶点对象并将它们放入数据库中。我仍然想知道,为什么它不工作?它的经典对象序列化(如果我保存字符串或哈希或其他一些通用结构,它会工作吗?)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-05-18
    相关资源
    最近更新 更多