0

I have the following set of Models / Associations:

enter image description here

Whilst this makes sense from on object oriented point of view, it introduces a huge distance between, for example; a NewsItem and the Photo.image that represents it in the UI.

An obvious improvement would be to dispense with MediaItem and make Video and Photo Polymorphic, however I need to treat them interchangeably, ordering, ranking them searching for them, and a Polymorphic association makes this vastly more complex / awkward. It would also involve many duplicated attributes (that are currently on MediaItem).

My main problem with this setup is that I have situations where a NewsItem is visually represented. Along with its title, I display an image which is the cover image of that NewsItem – effectively the first Photo in the gallery.

In this situation I am effectively doing this:

news_item.gallery.media_items.cover_photo.image.url(:thumbnail)

Given that there are up to 20 of these items on the screen at a time (possibly more), I definitely need to make sure I optimise the database queries by awkward chains of includes.

Of course I can go a large way to hiding some of the access behind delegation, for example:

news_item.gallery_cover_image

But this still leaves me with awkward queries trying to include the chain of tables.

Is there a more sensible way of structuring this?

Simone Carletti
  • 173,507
  • 49
  • 363
  • 364
Undistraction
  • 42,754
  • 56
  • 195
  • 331
  • what is the difference between `Photo` and `Video`? Wouldn't it be possible to only have the model `MediaItem` and differentiate the two types in a column? – davegson Nov 27 '14 at 11:04
  • Or on further thought: couldn't you just get rid of the `MediaItem` Model? Leading to Gallery: `has_many :photos` & `has_many :videos` – davegson Nov 27 '14 at 11:07
  • @TheChamp Video contains multiple attributes only related to videos and photo contains multiple attributes only related to photos. I don't want to throw them all into a single model where half of the attributes are irrelevant to the instance. Getting rid of `Media` item would mean 1. Duplicated fields between `Video` and `Photo` and 2. Would make it impossible to query for lists of MediaItems, which are ranked and ordered in numerous ways - for example a gallery might contain and display [photo, video, photo, photo, video]. – Undistraction Nov 27 '14 at 11:11
  • Ok that makes sense, clears it up. – davegson Nov 27 '14 at 11:13

0 Answers0