Forum: Ruby on Rails [Req] polymorphic associations howto

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.
E2ea3825e6e037e45b490d0c41e0d7b9?d=identicon&s=25 Fabien Franzen (Guest)
on 2006-02-10 10:36
(Received via mailing list)
Hi people,

I'm looking into polymorphic associations as a way to cleanup some
messy parts of my code. I think they will allow me to do exectly what
I need, but since there's little to none written on this subject, I'd be
delighted if someone could shed some light on this. Especially what
the different options and needed table structures need to be.

Thanks,

- Fabien
00973881979aa0a660ffbbb2f7a907fb?d=identicon&s=25 Peter De Berdt (Guest)
on 2006-02-10 15:09
(Received via mailing list)
On 10 Feb 2006, at 10:35, Fabien Franzen wrote:

> Hi people,
>
> I'm looking into polymorphic associations as a way to cleanup some
> messy parts of my code. I think they will allow me to do exectly what
> I need, but since there's little to none written on this subject,
> I'd be
> delighted if someone could shed some light on this. Especially what
> the different options and needed table structures need to be.

Chad Fowler has a rails recipe in the works talking about polymorphic
associations. It should be in one of the next beta versions of the
book. I can highly recommend it, it goes there where the Agile book
stops.

Best regards

Peter De Berdt
821395fe70906c8290df7f18ac4ac6cf?d=identicon&s=25 Rick Olson (Guest)
on 2006-02-10 16:59
(Received via mailing list)
On 2/10/06, Peter De Berdt <peter.de.berdt@pandora.be> wrote:
> > the different options and needed table structures need to be.
>
> Chad Fowler has a rails recipe in the works talking about polymorphic
> associations. It should be in one of the next beta versions of the
> book. I can highly recommend it, it goes there where the Agile book
> stops.
>
> Best regards
>
> Peter De Berdt

Polymorphic joins are kind of like an inverted STI setup.  Before, I'd
have to use STI to join a single Asset model to multiple models
(Articles, Books, Albums, for example).

class Asset < ActiveRecord::Base
end

class ArticleAsset < ActiveRecord::Base
  belongs_to :article, :foreign_key => 'object_id'
end

class Article < ActiveRecord::Base
  has_many :assets, :class_name => 'ArticleAsset'
end

Using polymorphic joins gets rid of the redundant *Asset models:

class Asset < ActiveRecord::Base
  # describes a generic 'attachable' class
  # uses attachable_id as foreign key, and attachable_type
  belongs_to :attachable, :polymorphic => true
end

class Article < ActiveRecord::Base
  has_many :assets, :as => :attachable
end

Don't let this stop you from getting the Rails Recipe book.  I can't
recommend it enough.  And, I'm quite sure Chad's explanation will be
much better ;)

--
Rick Olson
http://techno-weenie.net
E2ea3825e6e037e45b490d0c41e0d7b9?d=identicon&s=25 Fabien Franzen (Guest)
on 2006-02-11 07:17
(Received via mailing list)
>
> class Article < ActiveRecord::Base
>   has_many :assets, :class_name => 'ArticleAsset'
> end

Thanks Rick,

This is exactly the kind of info I'm looking for. I also used STI to
solve
this kind of thing before; it's the 'mess' I was talking about ;-)

Looking forward to see it revealed further in the beta book!

- Fabien
This topic is locked and can not be replied to.