Hi --

On Mon, 8 Sep 2008, Jarrett Green wrote:

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