所有嵌套后代深嵌套

阿尼什

我有包含所有父子记录的分类表:

<ActiveRecord::Relation [

#<Spree::Taxon id: 1, parent_id: nil, position: 0, name: "Brands", 
permalink: "brands", taxonomy_id: 1, lft: 1, rgt: 20, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil, 
description: nil, created_at: "2017-06-14 08:52:19", updated_at: "2017-06-23 06:34:11", 
meta_title: nil, meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 2, parent_id: nil, position: 0, name: "Brand", permalink: "brand", taxonomy_id: 2, lft: 21, rgt: 22, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil, description: nil, 
created_at: "2017-06-14 08:52:19", updated_at: "2017- 06-14 08:52:22", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 3, parent_id: 1, position: 0, name: 
"Bags", permalink: "brands/bags", taxonomy_id: 1, lft: 2, rgt: 3, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, 
icon_updated_at: nil, description: nil, created_at: "2017-06-14 
08:52:19", updated_at: "2017-06-21 05:03:52", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 4, parent_id: 1, position: 0, name: "Mugs", permalink: 
"brands/mugs", taxonomy_id: 1, lft: 4, rgt: 5, icon_file_name: nil, 
icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil, 
description: nil, created_at: "2017-06-14 08:52:20", updated_at: "2017-
06-14 08:58:29", meta_title: nil, meta_description: nil, meta_keywords: 
nil, depth: 0>, 

#<Spree::Taxon id: 5, parent_id: 1, position: 0, name: 
"Clothing", permalink: "brands/clothing", taxonomy_id: 1, lft: 6, rgt: 
11, icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, 
icon_updated_at: nil, description: nil, created_at: "2017-06-14 
08:52:20", updated_at: "2017-06-23 06:34:11", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 6, parent_id: 5, position: 0, name: "Shirts", permalink: 
"brands/clothing/shirts", taxonomy_id: 1, lft: 7, rgt: 8, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, 
icon_updated_at: nil, description: nil, created_at: "2017-06-14 
08:52:20", updated_at: "2017-06-20 06:10:16", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 7, parent_id: 5, position: 0, name: "T-Shirts", permalink: 
"brands/clothing/t-shirts", taxonomy_id: 1, lft: 9, rgt: 10, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, 
icon_updated_at: nil, description: nil, created_at: "2017-06-14 
08:52:20", updated_at: "2017-06-23 06:34:11", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 8, parent_id: 1, position: 0, name: "Ruby", permalink: 
"brands/ruby", taxonomy_id: 2, lft: 12, rgt: 13, icon_file_name: nil, 
icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil, 
description: nil, created_at: "2017-06-14 08:52:21", updated_at: "2017-
06-20 06:09:31", meta_title: nil, meta_description: nil, meta_keywords: 
nil, depth: 0>, 

#<Spree::Taxon id: 9, parent_id: 1, position: 0, name: 
"Apache", permalink: "brands/apache", taxonomy_id: 2, lft: 14, rgt: 15, 
icon_file_name: nil, icon_content_type: nil, icon_file_size: nil, 
icon_updated_at: nil, description: nil, created_at: "2017-06-14 
08:52:21", updated_at: "2017-06-23 06:34:11", meta_title: nil, 
meta_description: nil, meta_keywords: nil, depth: 0>, 

#<Spree::Taxon id: 10, parent_id: 1, position: 0, name: "Spree", permalink: 
"brands/spree", taxonomy_id: 2, lft: 16, rgt: 17, icon_file_name: nil, 
icon_content_type: nil, icon_file_size: nil, icon_updated_at: nil, 
description: nil, created_at: "2017-06-14 08:52:21", updated_at: "2017-
06-14 08:58:29", meta_title: nil, meta_description: nil, meta_keywords: 
nil, depth: 0>, ...]> 

我需要这样的树结构:

parent1(first taxon(first record) with all child if it has)
  - child1
  - child2
  - child3
    - child7
    - child9
  - child4
  - child5
parent2(it has no child)
parent3(Not it has child)
  - child7
  - child9

我简而言之,我需要每个单独的分类单元及其嵌套的子项。

我尝试过类似的事情:

Taxon.all.each do |i| 
     i.self_and_descendants.each do |j|
       taxon_with_childs << j
     end

但这给出了普通数组,但我想要带有父子嵌套的数组。

我也试过:

Taxon.each_with_level(Taxon.root.self_and_descendants) do |taxon, level|
    taxon_with_childs << taxon
  end

但这只是给第一个记录的孩子,而不是深度嵌套。

用户8214535

我认为它可以通过 DFS 功能完成

def add_self_and_descendents(x, taxon_with_childs)
  taxon_with_childs << x
  x.descendants.each do |y|
    add_self_and_descendents(y, taxon_with_childs)
  end
end

Taxon.all.each do |i| 
  add_self_and_descendents(i, taxon_with_childs)
end

而且如果数据库越来越大,时间会很长。但是没有其他方法可以在短时间内做到这一点,因为数据库操作函数只使用常量值,例如Taxon.where(:parent_id => 1 )

希望这对你有用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Rails 使用深嵌套访问所有关系

来自分类Dev

嵌套后代模式匹配

来自分类Dev

Bash:有多少嵌套会话深?

来自分类Dev

XPath选择后代(嵌套除外)

来自分类Dev

解析所有嵌套值

来自分类Dev

使用嵌套的动态嵌套for循环(所有排列)

来自分类Dev

Rails 如何查询嵌套关联返回所有嵌套关联

来自分类Dev

返回嵌套列表的所有组合

来自分类Dev

是否可以嵌套所有功能?

来自分类Dev

如何打印嵌套对象的所有值

来自分类Dev

将所有嵌套对象与elasticsearch匹配

来自分类Dev

删除所有空的嵌套列表

来自分类Dev

SimpleXML Framework返回所有嵌套元素

来自分类Dev

获取嵌套字典的所有键

来自分类Dev

R中的嵌套循环,所有可能

来自分类Dev

如何提取嵌套JSON的所有元素?

来自分类Dev

Python嵌套列表,所有组合

来自分类Dev

如何删除所有嵌套的JSON对象?

来自分类Dev

删除集合的所有嵌套文档

来自分类Dev

Laravel嵌套所有json响应

来自分类Dev

设置嵌套的div以填充所有父母

来自分类Dev

从字典递归获取所有嵌套元素

来自分类Dev

查找所有嵌套的img元素

来自分类Dev

R中的嵌套循环,所有可能

来自分类Dev

迭代嵌套 json 中的所有值

来自分类Dev

嵌套 FormGroup 重置所有组

来自分类Dev

SQL查找所有后代

来自分类Dev

获取具有嵌套列表属性的List的所有值

来自分类Dev

LDAP查询以显示具有嵌套组的所有组