cash's avatar
Level 5

How to model a collection of different items

Hi! I would like to have in my application a collection of ordered items of various kinds. Let's say a folder which consists of an ordered collection of items and each item can either be a note or an image.

In the case of the folder and the item it is simple: folder -> hasMany items and item -> belongsTo folder. But how to model the relationship between item and note or image. Each item represents exactly either a note or an image. Would it be some kind of polymorphic relationship?

0 likes
3 replies
martinbean's avatar
Level 80

@cash If a model can be “either X or Y [or X]” then yes, what you would be describing is a polymorphic relationship. In your case:

  • You have a Collection model
  • A Collection has many Item models
  • An Item model has a polymorphic relation to whatever model that item points to
1 like

Please or to participate in this conversation.