Ancestry gem in Rails and Mutli Nesting

后端 未结 1 1500
滥情空心
滥情空心 2021-01-28 11:32

I am using the ancestry gem in rails to nest some comments, and what I wanted was for you to be able to get all comments and then have them all nested. How ever I get the follow

相关标签:
1条回答
  • 2021-01-28 11:57

    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:

    enter image description here

    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:

    enter image description here

    #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 } %>
    
    0 讨论(0)
提交回复
热议问题