How do I find all the relatives of a node in ancestry?

303 Views Asked by At

I am using the ancestry gem, and what I want to do is always detected all the related posts to the current post.

So my Post has_ancestry.

In a related posts section of my view, I have this:

<% 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>

While that approach is fine, it just checks one use case (i.e. if this post has a child). What I want to happen though is when the person clicks through to the child, on that show page it should show this post as a related post too.

In effect it would be a parent, but I don't want to have to do if post.has_parents?, then if post.has_children?, if post.has_siblings?, etc.

Ideally, I would just like for any relatives to be detected, and if there are relatives I want to loop through them all and display them all in a homogenous way.

How do I do that?

0

There are 0 best solutions below