# Grouping and acts\_as\_tree

**URL:** <https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686>\
**Category:** Rails\
**Created:** [March 24, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686 "2010-03-24T17:10:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 24, 2010, 5:10pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686/1 "2010-03-24T17:10:15Z")

</div>

hi,

im using acts\_as\_tree and im not sure on how to group find-result.

x = find.almost\_all

\<% for y in @x.ancestors.reverse %\> \<%= link\_to h(y.title), page %\> \> \<% end %\>

gives me a nice “breadcrum”. but how is it possible to group the list  
lets  
say by the 1level or second level?

thx

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 26, 2010, 6:39pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686/2 "2010-03-26T18:39:04Z")

</div>

Tom,

Maybe it’s just me but I don’t quite understand… Could you explain  
what  
you’re trying to achieve?

/Lasse

2010/3/24 tom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 26, 2010, 6:49pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686/3 "2010-03-26T18:49:36Z")

</div>

hi & thx 4 ur time:

x = find.almost\_all

\<% for y in @x.ancestors.reverse %\> \<%= link\_to h(y.title), y %\> \> \<% end %\>

results in:

Root \> child A \> child A1  
Root \> child A \>  
Root \> child B \> child B999 \> child C245  
Root \> child D \> child B41  
Root \> child A \> child Axyz  
…  
…  
…

how can i group the list above lets say by the second level, eg like  
this:

CHILD A  
Root \> child A \> child A1  
Root \> child A \>  
Root \> child A \> child Axyz

CHILD B  
Root \> child B \> child B999 \> child C245

CHILD D  
Root \> child D \> child B41

or even by length after grouping by title (CHILD A):  
Root \> child A \>  
Root \> child A \> child A1  
Root \> child A \> child Axyz

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 26, 2010, 8:08pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686/4 "2010-03-26T20:08:37Z")

</div>

You can do it like this:

# this array is just my representation of your data

paths = [[“Root”, “child A”, “child A1”],  
[“Root”, “child A”],  
[“Root”, “child B”, “child B999”, “child C245”],  
[“Root”, “child D”, “child B41”],  
[“Root”, “child A”, “child Axyz”]]

# you can do the grouping like this

groups = paths.group\_by{ |p| p[1] }

puts groups.inspect # this is a hash

/Lasse

2010/3/26 tom [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![tomtt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/e47c2d/32.png) [@tomtt](https://www.ruby-forum.com/u/tomtt)\
**Post date:** [March 26, 2010, 8:55pm UTC](https://www.ruby-forum.com/t/grouping-and-acts-as-tree/185686/5 "2010-03-26T20:55:28Z")

</div>

thx!
