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

  • Home
  • SEARCH
  • 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 8468221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:52:15+00:00 2026-06-10T15:52:15+00:00

A user can have many posts, and each post can have many comments. When

  • 0

A user can have many posts, and each post can have many comments. When visiting a user’s profile, you get a list of posts rendered like so in the view:

<%= render @posts %>

I want to also render the comments for each post by doing something like:

<%= render @comments %>

However, where would I declare @comments? If I declare it in user#show, then I’d get all the comments for the user, as opposed to the comments for a specific post.

So when a collection is rendered, does it access a controller for each instance? If so, what controller method is it using?

  • 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-10T15:52:17+00:00Added an answer on June 10, 2026 at 3:52 pm

    You can render each post’s comments inside the post partial:

    <% @posts.each do |post| %>
      <%= render post %>
      <%= render :partial => 'posts_comments', :collection => post.comments %>
    <% end %>
    

    This does not go to the controller on each post.comments statement, it just queries the DB.

    You can also eliminate the multiple queries by setting the @posts instance variable in the controller in the following way:

    @posts = @user.posts.includes(:comments)
    

    Then when you do post.comments it will not query the DB, but load the comments from the memory.

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

Sidebar

Related Questions

I have users, posts and comments. User can post only one comment to each
I have two model: User, Article A user can like or dislike many articles,
What is the best way to show friends' posts? Each user can have maximum
I have some users that can have many posts , and each of those
I have the models User, Post, Comment and Tag. User creates Post. Posts can
I'm creating a forum website, where each registered user can write many posts and
Take tables: User, Comment, Snippet. A User can have many Snippets. A Snippet can
I created 3 tables: User User-Group Group Where I can have a many-to-many relationship.
I have a data model where a User can like a Project, Suggestion, Comment
I have the following database. One user can write one or more posts. Votes

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.