asciicasts.com
24 Nov '12, 5am
#rails polymorphic associations
Above is a page from a site that has three different models: articles, photos and events. We’d like to enhance the site so that its users can add comments to an article, a photograph or an event. If we were just adding comments to one of the models, say articles we’d create a Comment model and have a has_many / belongs_to relationship between Article and Comment . Without polymorphic associations we’d have to create three different types of comment model so that each one could belong_to the appropriate model, which would create a lot of repetition in the code. Polymorphic associations allow us to create just one Comment model and have each comment know which other model it should be associated with.
Full article:
http://asciicasts.com/episodes/154-polymorphic-association