Performance depends on many parameters, for 'standard' use it should be no major difference but , I should use solution 1 (create a sql view) , it should be easier to implement, to maintain, ... just because mysql/postgresql takes care of at the db level.
@panthro If you have a feed, then it’s probably best to have some form of FeedItem model with a polymorphic relationship pointing to the models as you mention. This way, you can just query the last n feed items instead of trying to construct UNION queries—this is essentially what a polymorphic relation attempts to extrapolate.
Use a polymorphic relation means you can also support additional feed item types in the future without having to change the underlying SQL statements. If you were just doing UNION statements, then you’d need to update the queries to support any new feed item types, or update them if you added or changed the columns or a corresponding feed item type.
Thanks for the advice, if I needed to update a view it would only be in one place.
If I was to use polymorphic on their own dedicated table, something like feed table, then every time I inserted a row into an underlying table, I would also have to update the parent relationship on the feed table.
Is this what you would suggest doing.
@sr57 suggests a view, which would manage that aspect for me.
I'm really stuck here with which direction to go and appreciate your answers so far.
@panthro Yeah, you’d need to create a row in your “feed items” table as well as a row for the corresponding feed item model type. But if you use a polymorphic relation, it means you can make use of features like eager-loading relationships.