Ancestry gem in Rails and Mutli Nesting

2019-03-03 14:28发布

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 following when I put: @comments = post.comments.arrange_serializable into my comments controller index action and get the following result:

{
   "comments":[
      {
         "id":3,
         "comment":"284723nbrkdgfiy2r84ygwbdjhfg8426trgfewuhjf",
         "author":"asdasdasdas",
         "post_id":268,
         "ancestry":null,
         "created_at":"2014-06-17T19:23:04.667Z",
         "updated_at":"2014-06-17T19:23:04.667Z",
         "children":[
            {
               "id":4,
               "comment":"284723nbrkdgfiy2r84ygwbdjhfg8426trgfewuhjf",
               "author":"asdasdasdas",
               "post_id":268,
               "ancestry":"3",
               "created_at":"2014-06-17T19:24:02.408Z",
               "updated_at":"2014-06-17T19:24:02.408Z",
               "children":[

               ]
            }
         ]
      },
      {
         "id":5,
         "comment":"97ryhewfkhbdasifyt834rygewbfj,dhsg834",
         "author":"asdasdasd",
         "post_id":268,
         "ancestry":"4",
         "created_at":"2014-06-17T20:30:04.887Z",
         "updated_at":"2014-06-17T20:38:16.060Z",
         "children":[

         ]
      }
   ]
}

It's very apparent that comment with id: 5 is suppose to be in the array of children which sits in comment id: 4 which IS nested under comment with id: 3.

Can some one one tell me why arrange_serializable does not "multi nest" comments? or if there is another function to do this with.

1条回答
Evening l夕情丶
2楼-- · 2019-03-03 14:50

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 } %>
查看更多
登录 后发表回答