# Design decision: Dynamically adding data question

**URL:** <https://www.ruby-forum.com/t/design-decision-dynamically-adding-data-question/151355>\
**Category:** Rails\
**Created:** [October 20, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/design-decision-dynamically-adding-data-question/151355 "2008-10-20T17:37:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 20, 2008, 5:37pm UTC](https://www.ruby-forum.com/t/design-decision-dynamically-adding-data-question/151355/1 "2008-10-20T17:37:02Z")

</div>

I need to give users the ability to optionally add metadata to  
documents. Another way to state this is the fact that users need to add  
at least 5 categories to a document.

Basically what I want to do is dynamically add metadata (or categories)  
to a document on an ad hoc basis. Here are the options that I have  
thought of:

**Option 1:**  
Should I do this by dynamically creating new table columns in the  
database?

**Option 2:**  
Should I define 5 columns called  
attirbute1,attirbute2,attirbute3,attirbute4,attirbute5 and then only use  
and show them if the user requires the attributes.

**Option 3:**  
Should I create a metadata table that keeps track of the columns and the  
data associated with them?

What do you think is the best way to achieve this? Can you think of any  
other ways to easily add this functionality. The problem is that the  
functionality needs to be very generic.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 20, 2008, 6:30pm UTC](https://www.ruby-forum.com/t/design-decision-dynamically-adding-data-question/151355/2 "2008-10-20T18:30:37Z")

</div>

I would create a “meta” table with two columns: “data” and  
“document\_id”. This  
way is much more neat and tidy; each metadata attribute, for each  
document,  
will be it’s own row. Trying to dynamically add columns or whatever  
could  
become disasterous. Go easy on yourself and use an AR association like  
this:

Document  
has\_many :metas

Meta  
belongs\_to :document

Document.metas will return all of the metadata attributes associated  
with the  
document, however many there may be.

My naming skills suck, I think you can come up with a better model name  
than “meta”, but you get the idea. As far as being “very generic”… I  
don’t  
know what that’s supposed to mean. I hope this is generic enough for  
you.

---

<div class="post-metadata">

**Author:** ![davidstokar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/4da419/32.png) [@davidstokar](https://www.ruby-forum.com/u/davidstokar)\
**Post date:** [October 20, 2008, 6:59pm UTC](https://www.ruby-forum.com/t/design-decision-dynamically-adding-data-question/151355/3 "2008-10-20T18:59:25Z")

</div>

Sent from my iPhone

On 20 Oct 2008, at 16:37, David S. \<rails-mailing-list@andreas-  
[s.net](http://s.net)\> wrote:

> **Option 1:**  
> Should I do this by dynamically creating new table columns in the  
> database?

Yuck

> **Option 2:**  
> Should I define 5 columns called  
> attirbute1,attirbute2,attirbute3,attirbute4,attirbute5 and then only  
> use  
> and show them if the user requires the attributes.

May not be flexible enough

> **Option 3:**  
> Should I create a metadata table that keeps track of the columns and  
> the  
> data associated with them?

I would go that way. Your metadata table just stores triples: id of  
the parent, the name of the piece of data and its value

Fred
