Skip to content
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

self_and_descendants returns empty collection when self is bottom-level node #299

Open
maxtedford opened this issue Feb 2, 2018 · 1 comment

Comments

@maxtedford
Copy link

maxtedford commented Feb 2, 2018

Hey Team,

This is probably more of a question than a definite issue, but it seems to me that the self_and_descendants association on a node is a bit misleading, in that whenever self is a childless node it seems to return an empty collection instead of a length-of-one collection containing only self.

I'm not sure if I'm missing something, but it seems to me that the INNER JOIN that's getting called as a result of that relation is actually trying to inner join the *_hierarchies table with an additional condition that the *_hierarchies.ancestor_id = $1, but since the node in question doesn't have any children, its ID (the $1 in the query) hasn't ever been added to the *_hierarchies table's ancestor_id column (though it is in the descendant_id column, obviously). This results in an empty table being returned, and thus an empty ActiveRecord::Assocations::CollectionProxy object.

Here's an example of the query that's invoked in my app when I call self_and_descendants on an AdministratorGroup object:

screenshot 2018-02-02 17 18 32

And here's a screenshot of my administrator_group_hierarchies table. As you can see, because the AdministratorGroup in question (which does exist in my administrator_groups table) is a bottom-level node, its ID (18) hasn't been added to the ancestor_id column in the administrator_group_hierarchies join table, and thus results in an empty inner join:

screenshot 2018-02-02 17 14 47

Given the above, am I off base with this interpretation? Is this the intended functionality? I'll be honest, I struggled a bit to find exactly where in your codebase the above SQL is being generated, though I'm happy to pursue opening a PR to address it if you do in fact believe it to be a bug. I would just need a little more context for how that query is being created before I do.

Thanks!

@mceachen
Copy link
Collaborator

mceachen commented Feb 4, 2018

Is the node persisted? If you look at your table, you'll see the where generations = 1 has a self-reference, specifically to make this query work.

Did I miss something?

(Also, please see this -- although this is an actively downloaded gem, there aren't many other developers here other than ones asking for support).

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

No branches or pull requests

2 participants