Ancestry gem in Rails and Mutli Nesting

♀尐吖头ヾ 提交于 2019-12-02 19:10:01

问题


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:


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


来源:https://stackoverflow.com/questions/24273419/ancestry-gem-in-rails-and-mutli-nesting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!