-
Notifications
You must be signed in to change notification settings - Fork 242
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
Conversation
Sounds good, can you add a changelog entry ? |
@seuros long delay here :) but I added to changelog. Can we could get this pr merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is helpful because we can more easily eager load ancestor_hierarchies than we can ancestors to avoid n + 1 queries.