Tuesday, April 5, 2016

closure_tree ancestors is giving ancestors in reverse order

Leave a Comment

I have a rails (4.2.3) app that uses closure_tree on the Post model

class Post < ActiveRecord::Base   has_closure_tree dependent: :destroy end 

The closure_tree gem documentation states that:

tag.ancestors is a ordered scope of [ parent, grandparent, great grandparent, … ].

I have a nested chain of posts (root to leaf) - 'Game', 'Walkthrough', 'Tutorial'

> tutorial_post.depth #=> 2 > [tutorial_post.parent.name, tutorial_post.parent.depth] #=> ["Walkthrough", 1] > [tutorial_post.parent.parent.name, tutorial_post.parent.parent.depth] #=> ["Game", 0] 

But when I query the ancestors of posts, I get the ancestors in reverse order (root to leaf, as opposed to the expected leaf to root).

> tutorial_post.ancestors.map(&:name) #=> ["Game", "Walkthrough"] 

I am almost certain that I have used the ancestors method in the past, and they do appear in the right order (in this case, the expected result is ["Walkthrough", "Game"]).

0 Answers

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment