Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8588265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T22:45:58+00:00 2026-06-11T22:45:58+00:00

I have an authors page that displays all the authors in the database. <h1>Listing

  • 0

I have an authors page that displays all the authors in the database.

<h1>Listing authors</h1>

<table>
  <tr>
    <th>Name</th>
    <th></th>
    <th></th>
    <th></th>
  </tr>

<% @authors.each do |author| %>
  <tr>
    <td><%= author.name %></td>
    <td><%= link_to 'Show', author %></td>
    <td><%= link_to 'Edit', edit_author_path(author) %></td>
    <td><%= link_to 'Destroy', author, method: :delete, data: { confirm: 'Are you sure?' } %></td>
  </tr>
<% end %>
</table>

<%= link_to 'New Author', new_author_path %>

And for each author, you click show, to bring up their own page.

<p>
  <b>Name:</b>
  <%= @author.name %>
</p>

<%= link_to 'Edit', edit_author_path(@author) %> |
<%= link_to 'Back', authors_path %>

Now I have the same set up for books, where the user can enter new books, show and edit books in the database.

I then set up a model called authorbooks that holds a relation ship between authors and books using has_many, and belongs_to in the models for author.rb, book.rb and authorbook.rb.

I am wanting the show page of the author to display every book that they are related to.

How would I go about this? I am new to rails and still learning so please remember when answering. Thanks in advance.

EDIT model code for each model:

author.rb

class Author < ActiveRecord::Base
  attr_accessible :name

  validates :name, :presence => true

  has_many :authorbooks
  has_many :books, :through => :authorbooks
end

book.rb

class Book < ActiveRecord::Base
  attr_accessible :name

  validates :name, :presence => true

  has_many :authorbooks
  has_many :authors, :through => :authorbooks
end

authorbook.rb

class Authorbook < ActiveRecord::Base
  attr_accessible :author_id, :book_id

  belongs_to :book
  belongs_to :author
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T22:45:59+00:00Added an answer on June 11, 2026 at 10:45 pm

    It would have been interesting to see the model code as well. I assume you have something like:

    class Author
      has_many :author_books
      has_many :books, :through => :author_books # this line might be missing,
                                                 # read in the api documentation about it.
    
    class AuthorBooks
      belongs_to :author
      belongs_to :book
    

    Now you can do something like:

    <h3>Related books</h3>
    
    <ul>
      <% @author.books.each do |book| %>
        <li><%= book.name %> <%= link_to "Details", book_path(book) %></li>
      <% end %>
    </ul>
    

    Without the :through line you could have done something like:

    @author.author_books.each do |ab|
      ... ab.book.name ...
    

    Note 1: you get N+1 load problems with the second example. See the eager loading chapter in A::R guide for more information about that.

    Note 2: Checkout HAML; much nicer than ERB

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a search.php page that successfully searches a database called sixcols and displays
I have a page where I search for the books that an author wrote
Each book can have many authors. And each author can author many books. class
I have the following HTML problem. Description I'm building an 'authors' page, and each
I have a database where i am displaying books. Main page displays 2 links
I currently have a plug in that displays the images of the authors that
I need something simple; I have page where a user clicks an author to
I have a table in sqlite db called [tblbook] with a column [authors]. What
I have a text box that will allow the user to enter multiple authors
So I have a module that displays articles. And I tried to make an

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.