# Serialized attribute converting to string on reload

**URL:** <https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139>\
**Category:** Rails\
**Created:** [January 6, 2010, 6:25am UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139 "2010-01-06T06:25:09Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![RailinAndWailin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@RailinAndWailin](https://www.ruby-forum.com/u/RailinAndWailin)\
**Post date:** [January 6, 2010, 6:25am UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139/1 "2010-01-06T06:25:09Z")

</div>

Hello,

Anybody help me out? I done googled it good and didn’t find anything  
(which should mean that I’ve done something wrong).

Not much code to talk about here, quite simply I have an attribute  
that is serialzied and when I update it, irb shows the object and my  
methods run on it as it were indeed so. Though when I reload it  
changes the data to a string. Not yaml, just one digit, the id of the  
object i had serialized. Don’t think that’s pertinent, most likely  
that was just the first listed attribute of the stored object.

Anybody have any idea why this would happen? (hirb styling in the  
below posted irb):

> Item.find(6).equip(me)  
> =\> true
> 
> > me.equipment  
> > ±—±-------------±-------------------------±------±------±------  
> > ±-----±------------------------±------------------------+  
> > | id | character\_id | rhand | lhand | chest | pants  
> > | head | created\_at | updated\_at |  
> > ±—±-------------±-------------------------±------±------±------  
> > ±-----±------------------------±------------------------+  
> > | 1 | 1 | #Weapon:0x7fa10db2b988 | | |  
> > | | 2010-01-06 05:09:21 UTC | 2010-01-06 05:09:51 UTC |  
> > ±—±-------------±-------------------------±------±------±------  
> > ±-----±------------------------±------------------------+  
> > 1 row in set
> 
> > me.equipment.save  
> > =\> true
> 
> > me.equipment.reload  
> > ±—±-------------±------±------±------±------±-----  
> > ±------------------------±------------------------+  
> > | id | character\_id | rhand | lhand | chest | pants | head |  
> > created\_at | updated\_at |  
> > ±—±-------------±------±------±------±------±-----  
> > ±------------------------±------------------------+  
> > | 1 | 1 | 6 | | | | |  
> > 2010-01-06 05:09:21 UTC | 2010-01-06 05:09:51 UTC |  
> > ±—±-------------±------±------±------±------±-----  
> > ±------------------------±------------------------+  
> > 1 row in set

---

<div class="post-metadata">

**Author:** ![RailinAndWailin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@RailinAndWailin](https://www.ruby-forum.com/u/RailinAndWailin)\
**Post date:** [January 6, 2010, 10:18am UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139/2 "2010-01-06T10:18:41Z")

</div>

On Jan 6, 5:23Â am, RailinAndWailin [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hello,
> 
> Anybody help me out? I done googled it good and didn’t find anything  
> (which should mean that I’ve done something wrong).

What’s in your model ?

Fred

---

<div class="post-metadata">

**Author:** ![RailinAndWailin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@RailinAndWailin](https://www.ruby-forum.com/u/RailinAndWailin)\
**Post date:** [January 6, 2010, 4:47pm UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139/3 "2010-01-06T16:47:20Z")

</div>

After trying this over and over I noticed that this only tends to  
happen with ActiveRecord::Base Objects.  
Any object that I created alone (in irb), i serialized and it worked  
out fine.

The model doesn’t constrain the serialized attribute to any sort of  
type, though it had originally (and then throw the  
SerializationTypeMimatch after reload). I’ve rewritten the setter  
several ways though there is no pre-assignment logic on the variable  
at all…no real way to be more specific without posting alot of code  
here.

I’ve tried serializing various instance of ActiveRecord models into  
this attribute and they all end up the same way. All except for  
classes not inherting from ActiveRecord::Base. I have serialized  
attributes (as Hashes) that work fine elsewhere…I dunno.

I even tried .to\_yaml when setting the attribute. Same thing keeps  
happening.

On Jan 6, 1:16Â am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![RailinAndWailin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@RailinAndWailin](https://www.ruby-forum.com/u/RailinAndWailin)\
**Post date:** [January 6, 2010, 4:59pm UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139/4 "2010-01-06T16:59:38Z")

</div>

On 6 Jan 2010, at 15:45, RailinAndWailin wrote:

> here.
> 
> I’ve tried serializing various instance of ActiveRecord models into  
> this attribute and they all end up the same way. All except for  
> classes not inherting from ActiveRecord::Base. I have serialized  
> attributes (as Hashes) that work fine elsewhere…I dunno.

This is probably to do with the reloading of activerecord classes. Why  
would you serialize an activerecord object rather than just storing the  
id (or a hash of its attributes)

Fred

---

<div class="post-metadata">

**Author:** ![RailinAndWailin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/a88e57/32.png) [@RailinAndWailin](https://www.ruby-forum.com/u/RailinAndWailin)\
**Post date:** [January 6, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/serialized-attribute-converting-to-string-on-reload/181139/5 "2010-01-06T17:13:06Z")

</div>

I had just the id in there before and could go back to doing that, I  
was looking for a “cleaner” (as in less code and query) way of going  
about it. Serializing it got rid of a couple of query’s which just  
obtained attributes (static class) from the object. I very well could  
just redo it with having Hashes instead… something like {id:=\>…,  
attr=\>…}.  
I just wondered if this may had been something I had done incorrectly  
or not.  
I suppose when I reload the model, ActiveRecord tries to reload the  
value of that field or something?

On Jan 6, 7:58Â am, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
