# Using Meta Data in a database field in a View

**URL:** <https://www.ruby-forum.com/t/using-meta-data-in-a-database-field-in-a-view/195784>\
**Category:** Rails\
**Created:** [September 30, 2010, 5:12am UTC](https://www.ruby-forum.com/t/using-meta-data-in-a-database-field-in-a-view/195784 "2010-09-30T05:12:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 30, 2010, 5:12am UTC](https://www.ruby-forum.com/t/using-meta-data-in-a-database-field-in-a-view/195784/1 "2010-09-30T05:12:26Z")

</div>

Hello…

Given a database with records like the following  
Table: AuditLog  
Fields: ID | USERID | TYPE | TIME | DATA

```
id:1, userId:1, type:PHOTO, time:2008-10-15 12:00:00, data:

```

{photo\_id:2089, photoname:“A trip to the beach”}

Lets say the database had 50 records, with Rails How can I loop  
through the results as follows:

- id, user\_id, data.photoID, data.PhotoName

The main thing I don’t get is how to extract what’s inside the data  
column. Example given:  
@mystuff = AuditLog.all  
How to display something like mystuff.data.photo\_id

Thanks

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 30, 2010, 6:32am UTC](https://www.ruby-forum.com/t/using-meta-data-in-a-database-field-in-a-view/195784/2 "2010-09-30T06:32:36Z")

</div>

Looks like I’m storing data correctly, I don’t want DATA to be a JSON  
object as it won’t be created via JavaScript but an Observer on  
certain models after\_create etc…

What do you think about using YAML? I was told Marshal was the way to  
go but after doing some google searches I found, there isn’t a lot of  
info on Marshal and it sounds like the community is against it…

That being said, in sum:

1. What is the best way to store unsecured data in the data column
2. How to insert and extract from the database?

Thank you

---

<div class="post-metadata">

**Author:** ![nobosh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/ecb155/32.png) [@nobosh](https://www.ruby-forum.com/u/nobosh)\
**Post date:** [September 30, 2010, 5:15pm UTC](https://www.ruby-forum.com/t/using-meta-data-in-a-database-field-in-a-view/195784/3 "2010-09-30T17:15:55Z")

</div>

nobosh wrote:

> Looks like I’m storing data correctly, I don’t want DATA to be a JSON  
> object as it won’t be created via JavaScript but an Observer on  
> certain models after\_create etc…
> 
> What do you think about using YAML? I was told Marshal was the way to  
> go but after doing some google searches I found, there isn’t a lot of  
> info on Marshal and it sounds like the community is against it…
> 
> That being said, in sum:
> 
> 1. What is the best way to store unsecured data in the data column

Use serialize.

> 1. How to insert and extract from the database?

Use serialize.

Or better yet, don’t. Take the time to design a schema that actually  
has the fields you need. If you’re doing a lot of stuff with flexible  
attributes, a non-SQL database such as MongoDB or CouchDB may be a  
better fit.

> Thank you

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
