Hash custom sort? Overriding <=>? Help!

This is a pretty ugly hash. The keys are objects. I’m afraid after going
down a few paths, it was a bit over my head. Here’s the hash:

[[#<Company id: 13, name: “Company Test 1”, created_at: “2008-09-03
04:05:58”, updated_at: “2008-09-03 04:05:58”, account_id: 16,
account_company: false>, [#<Project id: 34, name: “Test”, created_at:
“2008-09-07 04:54:55”, updated_at: “2008-09-07 04:54:55”, account_id:
16, primary_company_id: 13>]], [#<Company id: 12, name: “Company Test
2”, created_at: "…

Yeah. I know…

So briefly, the keys look like is #<Company id: 13, name: “Company Test
1”, … >.

I need to sort by ‘name’. Though another question remains that would
make this all moot. I use:

@project_companies = @projects.group_by(&:primary_company)

where :primary_company is a relationship situation. I found I can use
either the foreign key, (:primary_company_id, which would just give me
the id), or the above which gives me the whole enitre object as a key.
But is there any in between? Something like:

@project_companies = @projects.group_by(&:primary_company.name)

If not, how can I create my own sort using <=>??? I found a bit in the
rdoc, and well now I’m here.

Hi –

On Mon, 8 Sep 2008, Jarrett G. wrote:

Yeah. I know…
either the foreign key, (:primary_company_id, which would just give me
the id), or the above which gives me the whole enitre object as a key.
But is there any in between? Something like:

@project_companies = @projects.group_by(&:primary_company.name)

If not, how can I create my own sort using <=>??? I found a bit in the
rdoc, and well now I’m here.

Sorting and grouping aren’t the same thing, but if you want grouping
by the results of the name method, you would do:

@project_companies = @projects.group_by {|project| project.name }

David

David A. Black wrote:

Sorting and grouping aren’t the same thing, but if you want grouping
by the results of the name method, you would do:

@project_companies = @projects.group_by {|project| project.name }

Thanks for the response david! Unfortunately, I don’t think this is what
I was looking for. The key I need exists through an association.

Calling project.name gives me the project’s name, sure, and calling
project.primary_company_id, gives me the foreign key id of a has_one
relationship with Company. calling project.primary_company, through some
rails magic gives me the entire object associated with that foreign key.
I’d like that key to just be one parameter out of that relationship -
the associated company’s name, which was why I was asking if I could use
@project_companies = @projects.group_by(&:primary_company.name). It
seems to be an all or nothing. I can either have the foreign key
integer, or the entire object, but not param from that object?

2008/9/8 Jarrett G. [email protected]:

Thanks for the response david! Unfortunately, I don’t think this is what
integer, or the entire object, but not param from that object?
Why can’t you do

@project_companies = @projects.group_by {|pr| pr.primary_company.name}

?

robert