Using group_by in a Rails view

I have a blog application where I would like to display on the post view a list of keyword categories and associated keyword types.


<% @post.keywords.group_by(&:keyword_category_id).each do |category, type| %>
    <li><%= category %></li>
    <% type.each do |type| %>
        <%= type.keyword_type_id %>
    <% end %>
<% end %>

The keyword categories and keyword types are in separate models. The association for the models are:

has_many :keyword_types

    belongs_to :keyword_category

    has_many :keywords
    has_many :keyword_types, through: :keywords
    has_many :keyword_categories, through: :keywords
I am using a join table called 'keywords', for keeping track of the keyword categories and keyword types for a post.

The keywords table has the following fields.

The issue I am having is the view is displaying the ids and I can't figure out how to do something like Any ideas on what I am doing wrong? Thanks in advance for your help.

That's what your view is requesting.

<%= type.keyword_type_id %>

It's not clear from the model code you have posted where the name lives, and whether it is the name of the category you want to show or the name of the keyword. Could you post your schema? Usually, if you have a whatever_id available, you can also get at the whatever itself directly, and then branch off of that to show the particular property of that object that you want to show. Maybe it's this:

<%= %>

But I'm not sure, because your relationships seem to be one-sided.