Сериализованные атрибуты не возвращаются как правильные объекты - PullRequest
0 голосов
/ 01 ноября 2011

В моей модели есть сериализованный атрибут serialize :vertices, Array. Все выглядит нормально при его использовании, но после перезагрузки консоли (или по веб-запросу) сериализованный массив возвращается в виде строки, что явно не соответствует ожиданиям. Вот такая же процедура в консоли 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 > 

Я полагаю, что он не десериализован должным образом (возможно, из-за моих пользовательских классов в массиве?). Любые советы были бы очень полезны. Спасибо.

1 Ответ

0 голосов
/ 01 ноября 2011

Сохранение сериализованных экземпляров Ruby не совсем элегантно.Я бы рекомендовал более ортодоксальный подход, использующий, например, composed_of, или любое другое решение, которое фактически сохраняет аргументы для построения экземпляра в БД, а не сам экземпляр:

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

...