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 738329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:49:26+00:00 2026-05-14T07:49:26+00:00

I have created a simple blog application with Ruby on Rails. The applications consists

  • 0

I have created a simple blog application with Ruby on Rails. The applications consists of two tables, posts and comments. Comments belongs_to :post and posts has_many :comments.

I created posts table with the following columns: title:string, body:text.
I created the comments table with the following columns: body:text post_id:integer name:string email:string

In the /views/comments/index.html.erb display I would like to show a listing of all comments w/ the post title as well. Currently, the index view only displays post_id, body, name, email.

How do I replace the post_id column with the corresponding post title? Here is my code:

CommentsController Index action:

  def index
    @comments = Comment.all :order => "created_at DESC"
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @comments }
      format.json { render :json => @comments }
      format.atom
    end
  end

/views/comments/index.html.erb

<h1>Listing comments</h1>

<table>
  <tr>
    <th>Post</th>
    <th>Body</th>
  </tr>

<% @comments.each do |comment| %>
  <tr>
    <td><%=h comment.post_id %></td>
    <td><%=h comment.body %></td>
    <td><%=h comment.name %></td>
    <td><%=h comment.email %></td>
  </tr>
<% end %>
</table>

<br />
  • 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-05-14T07:49:27+00:00Added an answer on May 14, 2026 at 7:49 am

    If you have 100 comments, using comments.post.title with your code will result in 101 queries! See the Eager loading section on this docs page. Eager loading here will reduce it to 2.

    def index
      @comments = Comment.find(:all, :include => :post, :order => "created_at DESC")
      # ...
    end
    

    In your views, you can access the post title as

    <%= comment.post.title rescue "No post" %>
    

    Edit: I’m using rescue "No Post" because you have some comments with post_id = nil and some comments with post_id that point to posts that no longer exist.

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

Sidebar

Related Questions

I have created a blog application using Ruby on Rails which includes the ability
I have created a blog application using Ruby on Rails and have just added
I have built a blog application w/ ruby on rails and I am trying
I have just installed paperclip into my ruby on rails blog application. Everything is
I created a simple blog that has posts and comments. I want to find
For illustration purposes let us say I'm building a simple blog application and have
My Ruby on Rails application uses S3 and Paperclip. I have users upload a
I create a simple blog with cakephp where i have some posts and some
I have a simple blog app with the model Post. If I delete all
I have created a simple wcf service which used the WCF Service Library template.

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.