我有一个在 Post 模型上使用closure_tree 的 rails (4.2.3) 应用程序
class Post < ActiveRecord::Base
has_closure_tree dependent: :destroy
end
closure_tree gem文档指出:
tag.ancestors
是 的有序范围[ parent, grandparent, great grandparent, … ]
。
我有一个嵌套的帖子链(从根到叶)-“游戏”、“演练”、“教程”
> 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]
但是当我查询帖子的祖先时,我以相反的顺序得到祖先(根到叶,而不是预期的叶到根)。
> tutorial_post.ancestors.map(&:name) #=> ["Game", "Walkthrough"]
我几乎可以肯定我过去使用过祖先方法,并且它们确实以正确的顺序出现(在这种情况下,预期的结果是 ["Walkthrough", "Game"])。