Possible solutions to have a generic like system.
Polymorphic association is generally not recommended.
The table for
likes to have handle posts and comments becomes:
This is a bad idea because of how foreign keys are handled.
liked_id could not be used as a foreign key.
An alternative is that we now have a
comment_id column which both allow null values.
With this approach, you will likely want a check to make sure that only one of the two is set and at least one of the two is set.
This strategy was the one chosen for the example.
Just have two (or more) tables.
The one downside to this approaching is aggregating all the likes. It can still be done with a UNION or a VIEW.
Views are covered later in this application.