serialized attribute converting to string on reload

44 views
Skip to first unread message

RailinAndWailin

unread,
Jan 6, 2010, 12:23:50 AM1/6/10
to Ruby on Rails: Talk
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

Frederick Cheung

unread,
Jan 6, 2010, 4:16:29 AM1/6/10
to Ruby on Rails: Talk

On Jan 6, 5:23 am, RailinAndWailin <waugustyn...@gmail.com> 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

RailinAndWailin

unread,
Jan 6, 2010, 10:45:44 AM1/6/10
to Ruby on Rails: Talk
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 Cheung <frederick.che...@gmail.com>
wrote:

Frederick Cheung

unread,
Jan 6, 2010, 10:58:07 AM1/6/10
to rubyonra...@googlegroups.com

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

> 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.
>

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

> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To unsubscribe from this group, send email to rubyonrails-ta...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
>
>

RailinAndWailin

unread,
Jan 6, 2010, 11:11:15 AM1/6/10
to Ruby on Rails: Talk
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 Cheung <frederick.che...@gmail.com>

Reply all
Reply to author
Forward
0 new messages