2

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"]).

Anand
  • 3,690
  • 4
  • 33
  • 64
  • Do u use (or used previously) any additional hierarchy gems, like ancestry or nested set? Did u try to `Post.rebuild!` your tree? – twonegatives Apr 02 '16 at 06:51
  • No other hierarchy gems. I have not tried to rebuild my tree. Will try. – Anand Apr 03 '16 at 02:48
  • your data probably got somehow corrupted. Did you have a chance to try `Post.rebuild!` ? – VonD Apr 05 '16 at 15:23
  • I did a rebuild, but cannot currently verify it, due to some logistical issues - will verify and update here. – Anand Apr 05 '16 at 16:09
  • I'm finding the same problem since upgrading to Rails 4.2.6 from 4.0.4. Reloading the record from the db seems to help, but I haven't got to the bottom of what's happening yet. I think that maybe association ordering is somehow not happening on initial creation of a record, only when it's loaded from the db. That sounds reasonable, but it's weird that it's not been a problem before. – spume May 06 '16 at 11:31

0 Answers0