# Relationship Question (STI)

**URL:** <https://www.ruby-forum.com/t/relationship-question-sti/101801>\
**Category:** Rails\
**Created:** [February 6, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801 "2006-02-06T23:54:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [February 6, 2006, 11:54pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801/1 "2006-02-06T23:54:33Z")

</div>

My girlfriend and I have been dating for two years, and she just told me  
she  
has an STI…

Actually, I currently have three different models, like: Dog, Whale,  
Monkey  
They all have some similar attributes, but, they are unique enough to  
break into their own models. I could use STI, but I think the table  
would  
just have too many columns.

Now, I wanted to let the Dog’s, Whale’s and Monkey’s be buddies, and  
keep  
track of their relationships.

I created a Buddies table (with monkey\_id, dog\_id, whale\_id), and had  
has\_many relationships to link them all up.  
This will work… however, if I wanted to list all of a Monkey’s  
Buddies…  
it get’s tricky.  
I have to query the Buddies table to get the relationships, then query  
both  
the Whale and Monkey to get the appropriate objects back.

I saw Chad’s HABTM example using STI in the Recipes book… but I’m  
trying  
to stay away from STI.

# Any thoughts ? Also, sorry for the bizzare post… I have the flu !

Dylan

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [February 7, 2006, 6:28pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801/2 "2006-02-07T18:28:51Z")

</div>

On 2/6/06, Dylan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> # Any thoughts ? Also, sorry for the bizzare post… I have the flu !
> 
> Dylan

Dylan, if you don’t get an answer before then (soon!), I have a recipe  
or two already written for the next release of the book that I think  
will answer this for you. To do it cleanly, you’re probably going to  
want to be on Edge Rails.

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [February 7, 2006, 6:57pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801/3 "2006-02-07T18:57:04Z")

</div>

Ahh… thank you Chad !

I’ve been skimming the AR docs trying to conjure up a way to make this  
work,  
but to no avail thus far.  
In regard to the edge solution, I actually found a post by Tobias Lutke  
in  
which he described a few new features that could solve this… however,  
I  
can’t find the thread for the life of me.

I’ll keep plugging away at this (will get on edge a/s/a/p), but it would  
be  
great if you could share those recipes !.. or at least hint as to what  
features in edge to use 🙂  
BTW… I’m a proud owner of the PDF at the moment, and can’t wait for  
the  
end result. It’s great so far.

# Thanks !

Dylan

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [February 7, 2006, 11:44pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801/4 "2006-02-07T23:44:16Z")

</div>

Now, if I _could_ possibly squeeze my models into a STI setup, would it  
be  
better (performance wise) to use this instead of the :polymorphic  
approach ?

I’d assume so, as there is only one join table, and one self-referencing  
table… as opposed to the polymorphic approach, which looks like it  
could  
deal with a few more tables ?

---

<div class="post-metadata">

**Author:** ![Dylan\_S](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/34f0e0/32.png) [@Dylan\_S](https://www.ruby-forum.com/u/Dylan_S)\
**Post date:** [February 7, 2006, 7:09pm UTC](https://www.ruby-forum.com/t/relationship-question-sti/101801/5 "2006-02-07T19:09:49Z")

</div>

On 2/7/06, Dylan S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Ahh… thank you Chad !
> 
> I’ve been skimming the AR docs trying to conjure up a way to make this work,  
> but to no avail thus far.  
> In regard to the edge solution, I actually found a post by Tobias Lutke in  
> which he described a few new features that could solve this… however, I  
> can’t find the thread for the life of me.
> 
> I’ll keep plugging away at this (will get on edge a/s/a/p),

I’m also covering this in the next release of the book if it gives  
anyone any trouble with the current docs.

> but it would be  
> great if you could share those recipes !.. or at least hint as to what  
> features in edge to use 🙂

Look at polymorphic associations and join models. The source code for  
DHH’s acts\_as\_taggable plugin might give you some hints.

> BTW… I’m a proud owner of the PDF at the moment, and can’t wait for the  
> end result. It’s great so far.

Thanks!

–  
Chad F.  
[http://chadfowler.com](http://chadfowler.com)  
[http://pragmaticprogrammer.com/titles/fr\_rr/](http://pragmaticprogrammer.com/titles/fr_rr/) (Rails Recipes - In Beta!)  
[http://pragmaticprogrammer.com/titles/mjwti/](http://pragmaticprogrammer.com/titles/mjwti/) (My Job Went to India,  
and All I Got Was This Lousy Book)  
[http://rubycentral.org](http://rubycentral.org)  
[http://rubygarden.org](http://rubygarden.org)  
[http://rubygems.rubyforge.org](http://rubygems.rubyforge.org) (over one million gems served!)
