You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
How would one go about eager loading a categories collection's root categories? #337
Say I have a collection of categories, how would I go about eager loading all of their root categories (unless they're the root categories themselves) in one query?
changed the title [-]How would one go about eager loading a categories collection's root categories[/-][+]How would one go about eager loading a categories collection's root categories?[/+]on Nov 14, 2018
While solving similar problem, I've ended up using association through scenic view
I have created an SQL view which looked something like this:
SELECT
DISTINCT ON (descendant_id)
ancestor_id AS root_id,
descendant_id AS leaf_id,
generations AS depth
FROM category_hierarchies
ORDER BY descendant_id, generations DESC
Then I defined a read-only model which uses this view:
Activity
[-]How would one go about eager loading a categories collection's root categories[/-][+]How would one go about eager loading a categories collection's root categories?[/+]fizvlad commentedon Jun 15, 2022
(The question is 4 years, yeeeeah 🐧)
While solving similar problem, I've ended up using association through scenic view
I have created an SQL view which looked something like this:
Then I defined a read-only model which uses this view:
... and added an association to category model:
After that I was able to preload the root category:
This is probably can be done on Rails level with use of
ancestor_hierarchiesassociation, but it will probably look scarier