# Reconstructing ActiveRecord from YAML

**URL:** <https://www.ruby-forum.com/t/reconstructing-activerecord-from-yaml/109025>\
**Category:** Rails\
**Created:** [July 10, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/reconstructing-activerecord-from-yaml/109025 "2007-07-10T18:57:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [July 10, 2007, 6:57pm UTC](https://www.ruby-forum.com/t/reconstructing-activerecord-from-yaml/109025/1 "2007-07-10T18:57:38Z")

</div>

hello, please give me snipped, that reconstruct ActiveRecord object from  
YAML or Marshal.dump. When i type for example: YAML::load(item.to\_yaml),  
the returned object is not (item’s class) Item. When i use:  
Marshal::load(Marshal::dump item), it throws exception “undefined  
class/module Item” How can I do this? Thanx.

---

<div class="post-metadata">

**Author:** ![fero](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/b2d939/32.png) [@fero](https://www.ruby-forum.com/u/fero)\
**Post date:** [July 10, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/reconstructing-activerecord-from-yaml/109025/2 "2007-07-10T21:33:03Z")

</div>

Frantisek P. wrote:

> hello, please give me snipped, that reconstruct ActiveRecord object from  
> YAML or Marshal.dump. When i type for example: YAML::load(item.to\_yaml),  
> the returned object is not (item’s class) Item. When i use:  
> Marshal::load(Marshal::dump item), it throws exception “undefined  
> class/module Item” How can I do this? Thanx.

At a guess, a model’s attributes member is very Hash-like, and Hashes  
are also a native YAML type. So you _might_ store it with yaml\_string  
= model.attributes.to\_yaml , and load that with model.attributes =  
YAML::load(yaml\_string).

–  
Phlip  
[http://c2.com/cgi/wiki?ZeekLand](http://c2.com/cgi/wiki?ZeekLand) ← NOT a blog!!
