Hash custom sort? Overriding <=>? Help!

J

Jarrett Green

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.
 
D

David A. Black

Hi --

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.

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

--
Rails training from David A. Black and Ruby Power and Light:
Intro to Ruby on Rails January 12-15 Fort Lauderdale, FL
Advancing with Rails January 19-22 Fort Lauderdale, FL *
* Co-taught with Patrick Ewing!
See http://www.rubypal.com for details and updates!
 
J

Jarrett Green

David said:
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?
 
R

Robert Klemme

2008/9/8 Jarrett Green said:
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?

Why can't you do

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

?

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,602
Members
45,183
Latest member
OrderGlycoEase

Latest Threads

Top