Explain a polymorphic association?

Explain a polymorphic association?



Polymorphic associations allow a model to belong to more than one other model through a single association.

RoR QA polymorphic1
A polymorphic association schematic, from Rubyonrails.org


RoR QQ polymorphic2
Model structures of a polymorphic association, in Ruby code, from Ruybonrails.org
Here, the class Picture belongs_to both Employee and Product, but does so through a single association rather than through multiple.
Be sure to know an appropriate situation to create a polymorphic association, such as creating a comment model associated with multiple other models (articles, photos, etc.). The advantage of using polymorphism here is that it allows you to create a single comment model, rather than separate models for each one (PhotoComment model, ArticleComment model, etc.)

Comments

Popular posts from this blog

Discuss the seven characteristics of useful information.

Why have accounting software packages been designed with separate transaction modules?

A laboratory assistant prepared solution of 0.8 M, 0.6 M, 0.4 M, and 0.2 M sucrose, but forgot to label them. After realizing the error, the assistant randomly labeled the flasks containing these four unknown solutions as flask A, flask B, flask C, and flask D.