samedi 8 juillet 2017

all nested descendants Deep nesting

I have Taxons table with all parent child records

<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>, ...]>

I need a tree structure like this

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

i short i need each individual taxon with its nested child...

i have tried like

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

but this gives plain array but i want array with parent child nesting

i have also tried

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

but this is giving only first record's children and that to not deep nesting

Can any one please help me where i am wrong

Aucun commentaire:

Enregistrer un commentaire