Wednesday, April 30, 2008

Deserializing object failed in ActiveRecord

The serialize in ActiveRecord failed to deserialize object correctly

class ListTracker < ActiveRecord::Base
serialize :query_list
serialize :query
belongs_to :user
end

class QueryList
attr_accessor :class_name
def initialize(class_name = "")
@items = []
@restrict_items = []
@class_name = class_name.strip
@order_field = "id"
@order_direction = ""
@sort_items = []
@table_included = Set.new
end
def class_name
@class_name ||= ""
end
end


1.query_list = QueryList.new
2.list_tracker = ListTracker.new
3.list_tracker.query_list = query_list
4.list_tracker.save

In line 1 new QueryList object is created
In line 2 new ListTracker object is created
In line 3 QueryList object is assigned to query_list method(column in table which is serialized) of ListTracker an ActiveRecord decendent.
In line 4 ListTracker object is saved to table.

In Rails 1.2.3,

>> t = ListTracker.find(:first)

=> #"contacts", "updated_at"=>"2007-04-04 16:13:17", "query_list"=>"— !ruby/object:QueryList \nclass_name: Contact\nitems: \n - !ruby/object:Query \n after_blanket_count: 0\n before_blanket_count: 0\n join: OR\n key: id\n negation: false\n op: st\n table: contacts\n value: 62d703da-707c-11db-80be-000c6e8c7636\n - !ruby/object:Query \n after_blanket_count: 0\n before_blanket_count: 0\n join: OR\n key: id\n negation: false\n op: st\n table: contacts\n value: 1faad762-707c-11db-80be-000c6e8c7636\norder_direction: ''\norder_field: id\nrestrict_items: []\nsort_items: \n - !ruby/object:QuerySort \n descending: false\n field: created_at\n field_type: contacts\n summary: false\ntable_included: !ruby/object:Set \n hash: {}", "id"=>"64", "order_by"=>"", "user_id"=>"f7163ddc-fc4a-11da-81a0-000c6e8c7636"}>

>> t.query_list

=> #[#"st", "after_blanket_count"=>0, "value"=>"62d703da-707c-11db-80be-000c6e8c7636", "table"=>"contacts", "before_blanket_count"=>0, "negation"=>false, "key"=>"id", "join"=>"OR"}, @class="Query">, #"st", "after_blanket_count"=>0, "value"=>"1faad762-707c-11db-80be-000c6e8c7636", "table"=>"contacts", "before_blanket_count"=>0, "negation"=>false, "key"=>"id", "join"=>"OR"}, @class="Query">], "class_name"=>"Contact", "table_included"=>#, "restrict_items"=>[], "order_direction"=>"", "sort_items"=>[#"created_at", "summary"=>false, "descending"=>false, "field_type"=>"contacts"}, @class="QuerySort">], "order_field"=>"id"}, @class="QueryList">


Deserializing it gives a strange class of YAML::Object.

For some quick dirty fix Load the QueryList.. class before deserialize happens
I think rails dependency system didn't load the class before deserialize cause the problem.
change the ListTracker to:

class ListTracker < ActiveRecord::Base
QueryList
Query
serialize :query_list
serialize :query
belongs_to :user
end

hope this helps.

Source : Rails trac

1 comment:

Taylor Norrish said...

Hi I saw your profile on WorkingWithRails.com. Wanted to contact you about job.

Email me at taylor at PrintFriendly dot com.