# Is this a good STI approach?

**URL:** <https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810>\
**Category:** Rails\
**Created:** [September 30, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810 "2010-09-30T14:19:07Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 2:19pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/1 "2010-09-30T14:19:07Z")

</div>

Ive got 3 types of media. Event, photo and video.

Most of these share common columns (title, description, etc). I am  
thinking of creating a 4th model called media. And then event, photo  
and video will inherit from media, making media an STI.

Is this a good approach?  
I am worried that since all submissions are going into one table (the  
media table). Some db performance issues may arise?

What are your thoughts?

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 2:30pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/2 "2010-09-30T14:30:49Z")

</div>

On 30 September 2010 13:17, Christian F.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ive got 3 types of media. Event, photo and video.
> 
> Most of these share common columns (title, description, etc). I am  
> thinking of creating a 4th model called media. And then event, photo  
> and video will inherit from media, making media an STI.
> 
> Is this a good approach?

It is an option - for me, it would depend on how much other, different  
data each model has.  
Your structure may also suit a polymorphic approach - so that Event,  
Photo, and Video all have a “Media” association for the common fields.

> I am worried that since all submissions are going into one table (the  
> media table). Some db performance issues may arise?

How many Media do you plan to have?.. what sort of problems do you  
anticipate that you wouldn’t have with separate tables?

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 4:12pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/3 "2010-09-30T16:12:43Z")

</div>

Well its the primary nature of the site. So expect to have tons of  
media types. Initially, I thought separating them would keep the  
tables smaller since each media type has its own table.

Someone mentioned that having it as an STI is fine. Like any other app  
that becomes huge, we can always optimize?

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 6:44pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/4 "2010-09-30T18:44:20Z")

</div>

Marnen, in this case, what would you rather use? STI or polymorphism?

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 5:13pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/5 "2010-09-30T17:13:26Z")

</div>

Christian F. wrote:

> Well its the primary nature of the site. So expect to have tons of  
> media types. Initially, I thought separating them would keep the  
> tables smaller since each media type has its own table.

There’s generally no point to keeping tables smaller just for the sake  
of keeping them smaller. If you have proper indices and well-designed  
queries, your DB should be able to deal with large tables.

STI is perhaps smelly for other reasons (polymorphism or composition is  
often preferable), but in certain cases it really is the best way of  
doing things.

> Someone mentioned that having it as an STI is fine. Like any other app  
> that becomes huge, we can always optimize?

What’s to optimize?

## Best,

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

---

<div class="post-metadata">

**Author:** ![zurich\_by\_night](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/d78d45/32.png) [@zurich\_by\_night](https://www.ruby-forum.com/u/zurich_by_night)\
**Post date:** [September 30, 2010, 9:12pm UTC](https://www.ruby-forum.com/t/is-this-a-good-sti-approach/195810/6 "2010-09-30T21:12:46Z")

</div>

Please quote when replying.

Christian F. wrote:

> Marnen, in this case, what would you rather use? STI or polymorphism?

STI, polymorphism, or composition? It’s hard to tell from the  
information you’ve provided. It really depends on what sort of data  
your application stores and how the application is using that data.  
There’s no one answer, though I admit to a bias against STI for the  
reasons I gave in my previous post.

## Best,

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