Ancestry gem in Rails and Mutli Nesting

心已入冬 提交于 2019-12-02 10:55:51

Structure

Your arrange_serializable seems to be working - I think the problem is with how you're nesting the comments

We found out (took us ages) that if you want to use "nested" categories, you need to use a slash like this:

So if you're trying to "deep nest", you need to ensure you're including the whole route to the root object. Common logic would suggest "inheriting" from nested objects would also allow them to be nested - not so.

--

Fix

For your id 5, you should make the ancestry column this value:

$ rails c
$ comment = Comment.find 5
$ comment.update(ancestry: "3/4")

--

Partial

If you wanted to show a nested array of categories in your view, we use the following code:

#app/views/elements/_category.html.erb
<!-- Categories -->
<ol class="categories">
    <% collection.arrange.each do |category, sub_item| %>
        <li>
            <!-- Category -->
            <%= category.title %>

            <!-- Children -->
            <% if category.has_children? %>
                <%= render partial: "category", locals: { collection: category.children } %>
            <% end %>

        </li>
    <% end %>
</ol>


#app/views/application/index.html.erb
<%= render partial: "category", locals: { collection: Category.all } %>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!