# Advice for superset/subset data model

**URL:** <https://www.ruby-forum.com/t/advice-for-superset-subset-data-model/178544>\
**Category:** Rails\
**Created:** [November 6, 2009, 9:13am UTC](https://www.ruby-forum.com/t/advice-for-superset-subset-data-model/178544 "2009-11-06T09:13:43Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![danm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@danm](https://www.ruby-forum.com/u/danm)\
**Post date:** [November 6, 2009, 9:13am UTC](https://www.ruby-forum.com/t/advice-for-superset-subset-data-model/178544/1 "2009-11-06T09:13:43Z")

</div>

Hi,

I’m interested to know if anyone has any clever suggestions about how  
to model the following scenario with ActiveRecord:

- I’d like a collection of media, including photos, videos, and songs,  
all of which have some common and unique properties
- I start with a “media” table, that includes things like id, creation  
date, name, description, and filename
- the media table also has a media\_type\_id that maps to another table,  
media\_types, that has IDs for each of the three types of media
- then there are tables for each specific media type, so the photos  
table has photographer\_name, subject\_name, etc; the videos table has  
duration, format, etc; and the songs table has artist, duration, etc
- each of the specific tables also has an ID column; a media\_detail\_id  
column in the media table maps to this ID

My ultimate goal is for a URL like /media/index to display all the  
media names, and clicking on one gives the detail depending on what  
kind it is. Another goal is to be able to enter all the data for,  
say, a photo on one page – both the attributes for the media table  
(name, description) and for the photos table (photographer name,  
subject name).

I’ve been trying to work with Rails to do this simply, and it’s clear  
I’m fighting against the grain of how it likes to do things. If  
anyone has any suggestions, they’d be much appreciated.

Thanks,  
Dan

---

<div class="post-metadata">

**Author:** ![danm](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/8491ac/32.png) [@danm](https://www.ruby-forum.com/u/danm)\
**Post date:** [November 7, 2009, 3:59am UTC](https://www.ruby-forum.com/t/advice-for-superset-subset-data-model/178544/2 "2009-11-07T03:59:19Z")

</div>

This is a fairly typical polymorphic association. No need for the  
‘media\_types’ table at all. Each media table has a link to one photo,  
or one video, or one song. Call it ‘media\_detail’ for lack of a better  
term.

class Media \< ActiveRecord::Base  
belongs\_to :media\_detail, :polymorphic =\> true  
end  
class Photo \< ActiveRecord::Base # and same for Song and Video  
has\_one :media, :as =\> :media\_detail  
end

In your media table you have two columns: media\_detail\_type (string)  
and media\_detail\_id (integer).

As for views, I would suggest a separate controller for each type, and  
a shared partial view for the common fields.

It might be beneficial to use accepts\_nested\_attributes\_for :media  
inside each of Photo, Song, Video.
