Skip to content

Reference ancestor_hierarchies in depth instead of ancestors #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2023
Merged

Reference ancestor_hierarchies in depth instead of ancestors #398

merged 2 commits into from
Apr 11, 2023

Conversation

weilandia
Copy link
Contributor

This is helpful because we can more easily eager load ancestor_hierarchies than we can ancestors to avoid n + 1 queries.

tag = Tag.includes(:ancestor_hierarchies).first
tag.depth

@seuros
Copy link
Member

seuros commented Jul 31, 2022

Sounds good, can you add a changelog entry ?

@weilandia
Copy link
Contributor Author

@seuros long delay here :) but I added to changelog. Can we could get this pr merged?

Copy link
Member

@seuros seuros left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great

@seuros seuros merged commit 2b1f088 into ClosureTree:master Apr 11, 2023
Copy link

@Tranquility Tranquility left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you even need the includes? ancestors is a single query that returns all the ancestors.

@@ -77,7 +77,7 @@ def leaves
end

def depth
ancestors.size
ancestor_hierarchies.size

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code actually changes the behavior ancestor_hierarchies includes the self and ancestors does not. So this increases the result by 1.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we just do ancestor_hierarchies - 1 ?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this change is needed at all.

Copy link
Contributor Author

@weilandia weilandia Jan 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it so you can call depth on a node in a collection without an n+1 query (except it is off by 1 🤦).

Tag.includes(:ancestor_hierarchies).map(&:depth)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weilandia can you fix it and add a small test ? i will release a new version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants