# Storing different types of data in 1 model

**URL:** <https://www.ruby-forum.com/t/storing-different-types-of-data-in-1-model/186732>\
**Category:** Rails\
**Created:** [February 25, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/storing-different-types-of-data-in-1-model/186732 "2008-02-25T16:13:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 25, 2008, 4:13pm UTC](https://www.ruby-forum.com/t/storing-different-types-of-data-in-1-model/186732/1 "2008-02-25T16:13:24Z")

</div>

Hi all,

I’m creating a statistics system for events. When an event has passed,  
an admin can enter statistics for that passed event. Every user  
attending the event and the event itself have statistics. The problem  
is that depending on the type of the event, the type of statistics for  
both users and event can change. eg: for one event the number of  
remarks and time present will be registered for another event the name  
of the company he represents

I’m struggling on how to implement this especially database wise. I  
can ofcourse create a table that contains all possible statistics data  
and than only fill in the rows that matter for that event. the  
downside is that I lose a lot of storage since a lot of rows are left  
blanc.

maybe someone has an idea on how to solve this?

thanks in advance.  
Stijn

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 25, 2008, 7:28pm UTC](https://www.ruby-forum.com/t/storing-different-types-of-data-in-1-model/186732/2 "2008-02-25T19:28:47Z")

</div>

Tarscher wrote:  
The problem

> is that depending on the type of the event, the type of statistics for  
> both users and event can change. eg: for one event the number of  
> remarks and time present will be registered, for another event the name  
> of the company he represents

You could approach this with a key-key value type of table definition.

STATISTIC:  
EVENT\_ID  
STAT  
STAT\_TYPE  
STAT\_STRING

EVENT\_ID can link back to the event  
STAT is the name of each statistic, like REMARK\_COUNT, TIME\_PRESENT, or  
COMPANY\_NAME, however many you’d like to keep (I’d probably keep the  
valid list in another table).  
STAT\_TYPE is the data type of the value “stored”, which directs you to  
which of the remaining fields holds the valid value for this record.  
STAT\_STRING is the stringified version of the data.

You could record any number of different statistics for any event, it’s  
row-heavy, column-light, and has some of its own issues, but it’s  
workable.

I’m sure there’s a better approach, but it’s been like 10 years since I  
worked on an issue where I wanted to store an arbitrary collection of  
attributes.

---

<div class="post-metadata">

**Author:** ![Tarscher](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/8491ac/32.png) [@Tarscher](https://www.ruby-forum.com/u/Tarscher)\
**Post date:** [February 25, 2008, 7:30pm UTC](https://www.ruby-forum.com/t/storing-different-types-of-data-in-1-model/186732/3 "2008-02-25T19:30:50Z")

</div>

Ar Chron wrote:

> STAT\_TYPE is the data type of the value “stored”, which directs you to  
> which of the remaining fields holds the valid value for this record.

Arrgh… no edit…

STAT\_TYPE is the data type of the value stored, which tells you the  
transformation to apply to STAT\_STRING…
