Group By Hash Problem. Help!

I have a current_user via resthul_auth thast HABTM projects. In my
projects_controller’s index action, I have

def index
@page_title = “All Projects”
if current_user.admin?
@projects = current_account.projects
else
@projects = current_user.projects
end

@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!!!

On 8 Sep 2008, at 21:32, Jarrett G. wrote:

 @projects = current_user.projects

: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