# Group\_by missing items

**URL:** <https://www.ruby-forum.com/t/group-by-missing-items/162982>\
**Category:** Rails\
**Created:** [March 16, 2009, 12:34pm UTC](https://www.ruby-forum.com/t/group-by-missing-items/162982 "2009-03-16T12:34:41Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![kadoudal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/f0a364/32.png) [@kadoudal](https://www.ruby-forum.com/u/kadoudal)\
**Post date:** [March 16, 2009, 12:34pm UTC](https://www.ruby-forum.com/t/group-by-missing-items/162982/1 "2009-03-16T12:34:41Z")

</div>

I use the acts\_as\_tree plugin, and I try to display a list of the  
elements w group\_by

in my controller  
…  
companies = Company.find :all, conditions =\> Company.visible\_by  
(User.current), :include =\> :parent  
respond\_to do |format|  
format.html {  
@company\_tree = companies.group\_by {|p| p.parent || p}  
}

I have the company ids : [1, 2, 3, 4, 5, 6]  
with the following tree structure : 1 children -\>[2 , 3] 2 -\>  
children [4, 5, 6] and 3 has no child

the companies.group by gives me correctly an Hash with  
[key company 1 -\> value [company 2, company 3]  
key company 2 -\> value [company 4, company 5, company 6] ]

but no key company 3 … is it because the group is empty (no  
child) … how can I get the key company 2, with an empty array as  
value ? is it possible ?

thanks for your help
