From: Robert Klemme Date: 2008-09-09T22:05:09+09:00 Subject: Re: Hash custom sort? Overriding <=>? Help! 2008/9/8 Jarrett Green : > 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? Why can't you do @project_companies = @projects.group_by {|pr| pr.primary_company.name} ? robert -- use.inject do |as, often| as.you_can - without end