# Serialize not working as expected. Am I doing it wrong?

**URL:** <https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747>\
**Category:** Rails\
**Created:** [July 18, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747 "2008-07-18T21:56:40Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 18, 2008, 9:56pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/1 "2008-07-18T21:56:40Z")

</div>

I reported the bug here:  
[http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/647-serialize-d-array-not-unserializing-properly](http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/647-serialize-d-array-not-unserializing-properly)

But the replies are to the tone of “it’s not a bug”. Serialize isn’t  
working how I expect it to, but I was wondering if I’m just looking at  
it all wrong.

The code I’m using is essentially what you see in the bug report.

Thanks.

---

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 18, 2008, 10:00pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/2 "2008-07-18T22:00:45Z")

</div>

The serialized Array is getting unserialized correctly.

However, I think YAML needs to know about the Peg class before you can  
unserialize if you want the elements of the array to be Peg  
instances. Like someone at the rails bug tracker, I think you need to  
require and/or include peg.rb. One solution is to have the Peg class  
defined in board.rb before the Board definition (or maybe after).  
This worked for me:

#board.rb  
class Peg  
def meth  
“foo”  
end  
end

class Board \< ActiveRecord::Base  
serialize :pegs  
end

Loading development environment (Rails 2.1.0)

> > b = Board.new  
> > =\> #\<Board id: nil, pegs: nil, created\_at: nil, updated\_at: nil\>
> 
> > b.pegs  
> > =\> nil
> 
> > b.pegs = []  
> > =\> []
> 
> > b.pegs\<\< Peg.new  
> > =\> [#Peg:0xb7683adc]
> 
> > b.save  
> > =\> true
> 
> > b = Board.find 1  
> > =\> #\<Board id: 1, pegs: [#Peg:0xb766ec90], created\_at: “2008-07-18  
> > 19:58:01”, updated\_at: “2008-07-18 19:58:01”\>
> 
> > b.pegs.first.meth  
> > =\> “foo”
> 
> > quit

---

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 18, 2008, 10:17pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/3 "2008-07-18T22:17:43Z")

</div>

I mentioned in my original post on the bug tracker that you either  
need to include or require peg.rb (or write the class inside board.rb  
like you did).

My point is that it doesn’t seem like the expected behavior.

It seems like rails should automatically include/require the class for  
you, just like it always would. I’m not sure if that responsibility  
should be on YAML or on ActiveRecord, but it seems that it should be  
done automagically by one of the classes involved.

---

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 18, 2008, 11:40pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/4 "2008-07-18T23:40:59Z")

</div>

On Jul 18, 9:09 pm, “Jeremy (Auzigog)” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I mentioned in my original post on the bug tracker that you either  
> need to include or require peg.rb (or write the class inside board.rb  
> like you did).
> 
> My point is that it doesn’t seem like the expected behavior.
> 
> It seems like rails should automatically include/require the class for  
> you, just like it always would. I’m not sure if that responsibility  
> should be on YAML or on ActiveRecord, but it seems that it should be  
> done automagically by one of the classes involved.

I don’t think it can be ActiveRecord: it doesn’t understand yaml at  
all, it just gets a blob of data which it hands off to yaml. Yaml  
being a pure ruby thing doesn’t do the magic autoloading stuff that  
rails does and so you’re stuck.

Fred

---

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 19, 2008, 2:05am UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/5 "2008-07-19T02:05:42Z")

</div>

That’s the thing, YAML _does_ do all sorts of autoloading. All it has  
it text, and it turns that into the proper classes.

I do think you have a point about the separation, Frederick.

I would make the assumption that YAML has a list of folders it looks  
in when loading classes. If that were publicly accessible (IE - Rails  
could access it), then all rails would have to do is add the app/  
models/\* folders and app/lib/\* folders to that list of autoloading  
locations.

But I’m probably making too many assumptions there.

On Jul 18, 2:40 pm, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![Jeremy\_SAuzigog](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a183cd/32.png) [@Jeremy\_SAuzigog](https://www.ruby-forum.com/u/Jeremy_SAuzigog)\
**Post date:** [July 21, 2008, 5:19pm UTC](https://www.ruby-forum.com/t/serialize-not-working-as-expected-am-i-doing-it-wrong/196747/6 "2008-07-21T17:19:00Z")

</div>

I find it unexpected behavior that I read your ticket and didn’t read  
the solution you posted right before posting the exact same solution :-  
P

Anyways, you might want to check this out if you haven’t already:

[http://itsignals.cascadia.com.au/?p=10](http://itsignals.cascadia.com.au/?p=10)
