简体   繁体   English

Rails和Mutli嵌套中的祖先宝石

[英]Ancestry gem in Rails and Mutli Nesting

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. 我正在使用Rails中的祖先宝石来嵌套一些注释,而我想要的是让您能够获取所有注释,然后将它们全部嵌套。 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 = post.comments.arrange_serializable进入我的注释控制器索引操作并获得以下结果:

{
   "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 . 这是非常明显的是,与评论id: 5被假设是阵列中的children ,其坐落在评论id: 4 ,其根据与评论嵌套id: 3

Can some one one tell me why arrange_serializable does not "multi nest" comments? 有人可以告诉我为什么arrange_serializable不“多嵌套”注释吗? or if there is another function to do this with. 或是否还有其他功能可以执行此操作。

Structure 结构体

Your arrange_serializable seems to be working - I think the problem is with how you're nesting the comments 您的arrange_serializable似乎正在工作-我认为问题在于您如何嵌套评论

We found out (took us ages) that if you want to use "nested" categories, you need to use a slash like this: 我们发现(花了很长时间),如果要使用“嵌套”类别,则需要使用如下slash

在此处输入图片说明

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: 对于id 5 ,您应该使ancestry该值:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM