List Info

Thread: Rails ActiveRecord Question




Rails ActiveRecord Question
user name
2006-11-10 00:10:24
Here's some pseudo-code which implements a polymorphic
version of the example you listed in your original email. 
Note the intentional use of "collectible" (noun)
versus "collectable" (adjective).  I thought it
made more sense instead of using "stuff",
"stuffs", and "stuffable".  The actual
usage should help put the class definitions into focus.  The
Collectible object can have attributes and associations of
its own, for example, created_on or has_many :tags.

class User
  has_many :collectibles

class Collectible
  belongs_to :collectable, :polymorphic => true
  belongs_to :user

class Person
  has_many :collectibles, :as => :collectable

class Book
  has_many :collectibles, :as => :collectable


stuff = User.find(id).collectibles
details = stuff.collect { |widget|
  widget.collectable # returns a collectable object (i.e.
Person or Book)
}
# details should now be an array of people and books that
belong to User.find(id)

----- Original Message ----
From: Tim Pease <tim.peasegmail.com>
To: Brian Gibson <bwg1974yahoo.com>
Sent: Thursday, November 9, 2006 4:07:19 PM
Subject: Re: [Boulder-Denver Ruby Group] Rails ActiveRecord
Question

On 11/9/06, Brian Gibson <bwg1974yahoo.com> wrote:
> You should consider Polymorphic Associations or Single
Table Inheritence to accomplish this.
>
> STI is probably closer to your "one-table"
for all "stuff" requirement but if your different
kinds of stuff" have vastly different attributes your
table of "stuff" will be very wide.
>

STI is very much what I'm looking for, just without the
single table
;)  It can / will get very wide as new "stuff"
types are added.  I'd
much rather use two tables and have them treated as a single
object
(i'm some kind of "stuff" object, but you can find
all my details over
there in this other table).

I'll have to dig through the STI code and see if any ideas
pop out at me.

> Polymorphic Associations on the other hand will allow
you to create an association from one class to multiple
classes.
>

Thanks for the links on both of these. It'll take a me a
little while
to wrap my head around Polymorphic Associations. I don't
think it
quite fits my desires, though.

Blessings,
TwP





_______________________________________________
Bdrg-members mailing list
Bdrg-membersrubyforge.org
ht
tp://rubyforge.org/mailman/listinfo/bdrg-members
[1]

about | contact  Other archives ( Real Estate discussion Medical topics )