Using group_by in a Rails view

Walter,

Thanks for your reply.

I am going for a display like the following:

Fruit
Apple
Orange
Peach
Meat
Beef
Fish
Poultry

Fruit and Meat are category names and apple, orange, peach, etc are type names.

This information is being stored in a join table that look like the following:

post_id keyword_category_id keyword_type_id
1 1 1
1 1 2
1 1 3

The schema look like the following:

keywords table
id :integer
post_id: integer
keyword_category_id :integer
keyword_type_id :integer

keyword_categories table
id :integer
name: string

keyword_types table
id: integer
name: string
keyword_category_id :integer

I thought I could do the following:
.
<% @post.keywords.group_by(&:keyword_category).each do |category, type| %>

  • <%= category.keyword_category.name %>
  • <% type.each do |type| %> <%= type.keyword_type.name %> <% end %> <% end %>

    This code doesn’t work.

    I had to use &:keyword_category_id as in the code below. The output look like

    1
    1
    2
    3
    2
    4
    5
    6

    I want to display the category names and type names instead of the foreign key ids. I think I am using group_by wrong or even perhaps i need an association in the other direction. I am just baffled at the moment.

    Any ideas?

    Walter,

    Thanks for your reply.

    I am going for a display like the following:

    Fruit
      Apple
      Orange
       Peach
    Meat
      Beef
      Fish
      Poultry

    Fruit and Meat are category names and apple, orange, peach, etc are type names.

    This information is being stored in a join table that look like the following:

    post_id keyword_category_id keyword_type_id
    1 1 1
    1 1 2
    1 1 3

    The schema look like the following:

    keywords table
    id :integer
    post_id: integer
    keyword_category_id :integer
    keyword_type_id :integer

    keyword_categories table
    id :integer
    name: string

    keyword_types table
    id: integer
    name: string
    keyword_category_id :integer

    I thought I could do the following:
    .
    <% @post.keywords.group_by(&:keyword_category).each do |category, type| %>
      <li><%= category.keyword_category.name %></li>
        <% type.each do |type| %>
      <%= type.keyword_type.name %>
      <% end %>
    <% end %>
    This code doesn't work.

    I had to use &:keyword_category_id as in the code below. The output look like

    1
       1
       2
       3
    2
       4
       5
       6
       
    I want to display the category names and type names instead of the foreign key ids. I think I am using group_by wrong or even perhaps i need an association in the other direction. I am just baffled at the moment.

    Any ideas?

    You might want to look at this blog post (kind of old, but it might help): http://ariejan.net/2007/01/12/rails-group-results-by-week-using-group_by/

    You might also want to read through the Rails Guide on Associations, and see if you can parse out what's missing in your relationships. Make sure that each has_many is balanced on the other side by a belongs_to.

    Walter