# Loading & Dumping of data in AR with Marshal

**URL:** <https://www.ruby-forum.com/t/loading-dumping-of-data-in-ar-with-marshal/176985>\
**Category:** Rails\
**Created:** [October 9, 2009, 11:54am UTC](https://www.ruby-forum.com/t/loading-dumping-of-data-in-ar-with-marshal/176985 "2009-10-09T11:54:23Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![0x001a4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/b3f665/32.png) [@0x001a4](https://www.ruby-forum.com/u/0x001a4)\
**Post date:** [October 9, 2009, 11:54am UTC](https://www.ruby-forum.com/t/loading-dumping-of-data-in-ar-with-marshal/176985/1 "2009-10-09T11:54:23Z")

</div>

Hey,

I’m writing an application that stores models attributes in a single  
column called `data`, using Marshal. My ultimate goal is to allow any/  
all records to be saved in one database table. I’m wondering if  
there’s a better way to do this, and also if I should Base64 encode  
the result of Marshal.dump as mentioned here:  
[http://www.ruby-forum.com/topic/164786#723805](http://www.ruby-forum.com/topic/164786#723805)

Here’s my current code:

# Post(id: integer, data: text, type: string, created\_at: datetime,

updated\_at: datetime)  
class Content \< ActiveRecord::Base  
private  
def unmarshal  
Marshal.load(self.data)  
end  
end

class Post \< Content  
before\_save :marshal

def title  
unmarshal[:title]  
end

def title=(value)  
@title = value  
end

private  
def marshal  
self.data = Marshal.dump({ :title =\> self.title })  
end  
end

Thanks for any tips/help!

---

<div class="post-metadata">

**Author:** ![0x001a4](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/0/b3f665/32.png) [@0x001a4](https://www.ruby-forum.com/u/0x001a4)\
**Post date:** [October 10, 2009, 11:52pm UTC](https://www.ruby-forum.com/t/loading-dumping-of-data-in-ar-with-marshal/176985/2 "2009-10-10T23:52:35Z")

</div>

On Oct 9, 5:50 am, Mat H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hey,
> 
> I’m writing an application that stores models attributes in a single  
> column called `data`, using Marshal. My ultimate goal is to allow any/  
> all records to be saved in one database table. I’m wondering if  
> there’s a better way to do this, and also if I should Base64 encode  
> the result of Marshal.dump as mentioned here:[How to save Ruby object as a blob in MySQL - Ruby - Ruby-Forum](http://www.ruby-forum.com/topic/164786#723805)

This design is well on it’s way to ending up on TheDailyWTF. Several  
things:

- using Marshal is a bad, bad, bad idea. The format is fragile when  
class definitions are changed and not guaranteed to be readable across  
Ruby versions. It’s also completely incomprehensible to anything  
besides Ruby.

- you’re re-implementing the idea of serialized fields (check the  
ActiveRecord docs) using Marshal instead of YAML.

- you’re trying to jam all the data into one table. This is almost  
certainly wrong.

If your data set is REALLY as unstructured as you’re implying, you may  
want to look at some of the schemaless document DB stuff (CouchDB and  
friends).

–Matt J.
