.children performs incorrect SQL query #89

tilo opened this Issue Mar 19, 2012 · 0 comments


None yet

1 participant


ancestry 1.2.5 , activerecord 3.2.1

I'm generating tree-nodes in an STI-table using ancestry - the source is an XML feed in which the nodes are not ordered.

This leads to a problem when the nodes are created and linked.. they are not necessarily created in a top-down order.

Let's assume my tree is only three levels deep. If I have a node x from the mid-level and do x.children on it.
Order of creation was: Top-node, Leaf-node, MidLevel-node x.

# MidLevel node x:
=> 1041124
=> "1041096"

# this SQL seems incorrect:
      SELECT  `nodes`.* FROM `nodes` WHERE `nodes`.`ancestry` = 1041096/1041124'
 => []

# find the orphaned child:
Node.where("ancestry like '%1041124'")
 => [#<Node id: 1041123, ancestry: "1041124", ...>]

Shouldn't the SQL query in the example above be:

  SELECT  `nodes`.* FROM `nodes` WHERE `nodes`.`ancestry` LIKE '%/1041124' OR `nodes`.`ancestry` = '1041124'
@tilo tilo closed this Mar 20, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment