How Can I Improve This Structure Of Tables/Models?

61 views Asked by At

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?

0

There are 0 answers