Forum: Ruby on Rails Relationship Question (STI)

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-02-06 23:54
(Received via mailing list)
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
7ed3175d60e44aa7cb891973223e3998?d=identicon&s=25 Chad Fowler (Guest)
on 2006-02-07 18:28
(Received via mailing list)
On 2/6/06, Dylan Stamat <dylans@gmail.com> 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 Fowler
http://chadfowler.com
http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!)
http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India,
and All I Got Was This Lousy Book)
http://rubycentral.org
http://rubygarden.org
http://rubygems.rubyforge.org (over one million gems served!)
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-02-07 18:57
(Received via mailing list)
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
7ed3175d60e44aa7cb891973223e3998?d=identicon&s=25 Chad Fowler (Guest)
on 2006-02-07 19:09
(Received via mailing list)
On 2/7/06, Dylan Stamat <dylans@gmail.com> 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 Fowler
http://chadfowler.com
http://pragmaticprogrammer.com/titles/fr_rr/ (Rails Recipes - In Beta!)
http://pragmaticprogrammer.com/titles/mjwti/ (My Job Went to India,
and All I Got Was This Lousy Book)
http://rubycentral.org
http://rubygarden.org
http://rubygems.rubyforge.org (over one million gems served!)
Ff82af3238a57fbd1212832ec1a19f28?d=identicon&s=25 Dylan Stamat (Guest)
on 2006-02-07 23:44
(Received via mailing list)
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 ?
This topic is locked and can not be replied to.