Setting Up the Relationship

First, let’s establish the relationship between categories and books in the Rails model.
Make sure that the setup of models is like this:
Category Model

class Category < ApplicationRecord
  has_many :books
end

Book Model

class Book < ApplicationRecord
  belongs_to :category
end

With this setup, each category can have multiple books associated with it.

Fetching Categories and Books

Now, let’s move on to the controller. In the controller’s index action, we want to fetch all categories along with their associated books. To do this efficiently and avoid the N+1 query problem, we can use the includes method:

# Categories controller

def index
  @categories = Category.includes(:books).all
end

Displaying Categories and Books

In your view, loop through the categories and their associated books and display them as desired.
Here’s an example view:

<!-- Categories view -->
<% @categories.each do |category| %>
  <h2><%= category.name %></h2>
  <ul>
    <% category.books.each do |book| %>
      <li><%= book.name %></li>
    <% end %>
  </ul>
<% end %>

In this view:

  • We loop through @categories to iterate over each category.
  • Inside the category loop, we display the category name using an <h2> heading.
  • We use an unordered list (<ul>) to display the associated books as list items (<li>).
  • We use category.books.each to loop through the books associated with the current category and display their names.

Adding Links to Book Names

If needed, we can add links to the book names. We can modify the view like this:

<!-- Categories view for Book Names with Links -->
<% @categories.each do |category| %>
  <h2><%= category.name %></h2>
  <ul>
    <% category.books.each do |book| %>
      <li><%= link_to book.name, book %></li>
    <% end %>
  </ul>
<% end %>

Now, the book names will be displayed as links that lead to the respective book’s details page.

Support On Demand!

Ruby on Rails