Skip to content

Commit

Permalink
updated ancestor sql calls
Browse files Browse the repository at this point in the history
  • Loading branch information
dhoss committed Nov 3, 2014
1 parent c32ef8e commit 001b901
Showing 1 changed file with 4 additions and 16 deletions.
20 changes: 4 additions & 16 deletions lib/treeify.rb
Expand Up @@ -16,9 +16,8 @@ module Treeify

class_attribute :cols
scope :roots, -> { where(parent_id: nil) }

scope :tree_for, ->(instance) { self.find_by_sql self.tree_sql_for(instance) }
scope :tree_for_ancestors, ->(instance) { where("#{table_name}.id IN (#{tree_sql_for_ancestors(instance)})").order("#{table_name}.id") }
scope :tree_for_ancestors, ->(instance) { self.find_by_sql self.tree_sql_for_ancestors(instance) }

end

Expand Down Expand Up @@ -76,20 +75,9 @@ def tree_sql_for(instance)
end

def tree_sql_for_ancestors(instance)
"WITH RECURSIVE cte (id, path) AS (
SELECT id,
array[id] AS path
FROM #{table_name}
WHERE id = #{instance.id}
UNION ALL
SELECT #{table_name}.id,
cte.path || #{table_name}.id
FROM #{table_name}
JOIN cte ON #{table_name}.parent_id = cte.id
)
SELECT cte.id FROM cte WHERE cte.id != #{instance.id}"
"#{tree_sql(instance)}
SELECT * FROM cte
WHERE cte.id != #{instance.id}"
end
end

Expand Down

0 comments on commit 001b901

Please sign in to comment.