self_and_descendants returns empty collection when self is bottom-level node
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:
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:
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!
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).