Relationship Question (STI)


#1

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


#2

On 2/6/06, Dylan S. 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://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!)


#3

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 :slight_smile:
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


#4

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 ?


#5

On 2/7/06, Dylan S. 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 :slight_smile:

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://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!)