Today, I got a task to solve N+1 issue with polymorphic association. This is tricky as we want to avoid changing data structure - because it will take much more time to change established data structure. I have tried this solution from stackoverflow. However it doesn’t work as the expectation for the task.

At the end, inspired by this awesome article, I am finally able to solve the task. Even better, it is also works perfectly when you need to preload more polymorphic associations.