# Group By Hash Problem. Help!!

**URL:** <https://discuss.rubyonrails.org/t/group-by-hash-problem-help/34138>\
**Category:** rubyonrails-talk\
**Created:** [September 8, 2008, 8:32pm UTC](https://discuss.rubyonrails.org/t/group-by-hash-problem-help/34138 "2008-09-08T20:32:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [September 8, 2008, 8:32pm UTC](https://discuss.rubyonrails.org/t/group-by-hash-problem-help/34138/1 "2008-09-08T20:32:17Z")

</div>

I have a current\_user via resthul\_auth thast HABTM projects. In my projects\_controller's index action, I have

def index &nbsp;&nbsp;&nbsp;&nbsp;@page\_title = "All Projects" &nbsp;&nbsp;&nbsp;&nbsp;if current\_user.admin? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@projects = current\_account.projects &nbsp;&nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@projects = current\_user.projects &nbsp;&nbsp;&nbsp;&nbsp;end

&nbsp;&nbsp;&nbsp;&nbsp;@project\_companies = @projects.group\_by(&:primary\_company)

The problem is, the HASH can't be sorted - in my view, I group the projects by their primary company. The group\_by gives me a hash with a primary company object as the key. I can either use the :primary\_company\_id, which just gives me the integer, or the :primary\_company, which through some magic gives me a whole object as a hash key. Is there any inbetween? something like:

@project\_companies = @projects.group\_by(&:primary\_company.name) (which didn't work)

that will only give me the name string from that object as a hash key? Otherwise the view isn't in alphabetical order, or in any order really. I'm kind of stuck!!!!

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [September 9, 2008, 1:44am UTC](https://discuss.rubyonrails.org/t/group-by-hash-problem-help/34138/2 "2008-09-09T01:44:18Z")

</div>

> I have a current\_user via resthul\_auth thast HABTM projects. In my projects\_controller's index action, I have
> 
> def index &nbsp;&nbsp;&nbsp;@page\_title = "All Projects" &nbsp;&nbsp;&nbsp;if current\_user.admin? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@projects = current\_account.projects &nbsp;&nbsp;&nbsp;else &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@projects = current\_user.projects &nbsp;&nbsp;&nbsp;end
> 
> &nbsp;&nbsp;&nbsp;@project\_companies = @projects.group\_by(&:primary\_company)
> 
> The problem is, the HASH can't be sorted - in my view, I group the projects by their primary company. The group\_by gives me a hash with a primary company object as the key. I can either use the :primary\_company\_id, which just gives me the integer, or the :primary\_company, which through some magic gives me a whole object  
> as a hash key. Is there any inbetween? something like:
> 
> @project\_companies = @projects.group\_by(&:primary\_company.name) (which didn't work)

You need to undestant that &:primary\_company is just shorthand which  
is (basically even though that's not quite how it's implemented)  
equivalent to

@project\_companies = @projects.group\_by {|project|  
project.primary\_company }

and now it's easy to change that to

@project\_companies = @projects.group\_by {|project|  
project.primary\_company.name }

Fred
