2017-07-08 59 views
1

我有分類羣表與所有的父子記錄:所有嵌套的後代深度嵌套

<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 

但這僅給出第一個記錄的孩子,而不是一個深度嵌套。

+0

不,我不使用... – Anish

+0

@spickermann你有什麼想法如何解決這個問題 – Anish

+0

'Spree :: Taxon'在內部使用'acts_as_nested_set'(參見:[GitHub上的代碼](https:// github的.com /禮包/禮包/斑點/主/核心/應用/模型/禮包/ taxon.rb#L10))。你能添加一個預期輸出的例子嗎?我們需要一個嵌套數組還是一個平面數組? – spickermann

回答

2

我認爲它可以通過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)

希望這對你的作品。