Skip to content
This repository

.children performs incorrect SQL query #89

Closed
tilo opened this Issue March 18, 2012 · 0 comments

1 participant

Tilo
Tilo
tilo commented March 18, 2012

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:
x.id
=> 1041124
x.ancestry
=> "1041096"

# this SQL seems incorrect:
x.children
      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 March 19, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.