Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 我如何找到祖先中一个节点的所有亲属?_Ruby On Rails_Ruby On Rails 4_Ancestry - Fatal编程技术网

Ruby on rails 我如何找到祖先中一个节点的所有亲属?

Ruby on rails 我如何找到祖先中一个节点的所有亲属?,ruby-on-rails,ruby-on-rails-4,ancestry,Ruby On Rails,Ruby On Rails 4,Ancestry,我正在使用祖先宝石,我想做的是总是检测到当前帖子的所有相关帖子 所以我的帖子有它的血统 在我的观点的相关帖子部分,我有以下内容: <% if @post.has_children? %> <p> <ul> <% @post.children.each do |child| %> <li><%= link_to child.title, post_path(child)%&g

我正在使用祖先宝石,我想做的是总是检测到当前帖子的所有相关帖子

所以我的
帖子有它的血统

在我的观点的
相关帖子
部分,我有以下内容:

<% if @post.has_children? %>
    <p>
        <ul>
    <% @post.children.each do |child| %> 
            <li><%= link_to child.title, post_path(child)%></li>        
    <% end %>
        </ul>
    </p>
<% else %>
    <p>
        There are no related posts.
    </p>
<% end %>
</div>


没有相关职位。

虽然这种方法很好,但它只检查一个用例(即,如果这篇文章有一个子项)。但是我想发生的是,当这个人点击到这个孩子的时候,在这个显示页面上,这个帖子也应该显示为一个相关的帖子

实际上,它将是一个家长,但我不想做
如果post.has\u parents?
,那么
如果post.has\u children?
如果post.has\u children?
,等等

理想情况下,我只希望检测到任何亲属,如果有亲属,我希望遍历所有亲属,并以同质方式显示所有亲属


我该怎么做?

你想在祖先的链条上走多远?只有父母、兄弟姐妹和孩子吗?@nikkon226是的。问题是,当用户没有父节点时,树上的所有其他节点都是同级节点,这没有多大意义。请尝试此
@post.path.from\u depth(-1).to\u depth(1)。每个节点都
。我没有安装祖先,所以它可能需要调整。