Skip to content

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.


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