Having some trouble wrapping my head around how I could use Polymorphic associations like this.
Say I have a abstract Posts
schema.
Then say I have all kinds of variations of posts like VideoPosts
or ImagePosts
ect.
I want to use the Posts
schema as the common abstract record and then create the respective record with its more specific values.
That way I can do a simple query on all Posts
where I need the common values regardless of their specific type but without doing complex UNION
queries.
The common examples give the example of using comments on posts. The issue Iām running into is that is backwards for my type of associations and Iām not sure how I can flip it.
Ideally I will be able to create a record for any of the Complex schemas and have created a Abstract record that abstracts our ācommon fieldsā for that given Complex schema and have created a relation between the two records that allow for me to be able to query across all the complex schemas without building large and difficult to manage a UNION query to get all the common fields for our different complex schemas (IE just look up the Abstract table).
Issues Iād like to avoid:
- Having to create large manifest of belongs_to/has_n per N number of columns for N number of Complex schemas my system may end up with.
- Having to create and manage a large complex
UNION
query to look over all of the Complex schemas to get an aggregation of all the complex schemas. (ie looking up counts for all complex schemas ect.) - Coupling logic of the abstract layer to the complex layer. Say one complex schema has a wildly different need then that of another complex schema. I donāt want that concern to bleed into the abstract layer
I also created an example repo that Iām gonna use to explore this issue.
Thoughts?