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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T00:17:16+00:00 2026-06-07T00:17:16+00:00

I have an application with Users, Posts and Comments. Users has_many Posts, Posts has_many

  • 0

I have an application with Users, Posts and Comments. Users has_many Posts, Posts has_many Comments and Comments belong_to Users and Posts.

In my view template I’m looping over the Comments like this to get the username of the commenter:

User.find(comment.user_id).name

How efficient is this if I’m doing this for every Comment per Post ?
I could imagine that it’s maybe faster to store the username in a separate row in the Comments table, but duplicating the data seems just wrong.

Am I being paranoid and is ActiveRecord doing some caching magic or is there a better way of doing something like this ?

  • 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-07T00:17:18+00:00Added an answer on June 7, 2026 at 12:17 am

    You can preload the users in the initial query.
    Seeing as you have the association setup between User and Comment you can access the user via the association.

    Let’s assume you have a controller method:

    def show
      @posts = Post.limit(10).includes(:comments => :user)
    end
    

    In your view as you loop over @comments:

    @posts.each do |post|
      # show post stuff
      post.comments.each do |comment|
        comment.user.name
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have the following tables: Posts id title content Users id
My application has news, posts, comments and various other common things. I'd like to
I have on my webpage the code for comments like this: <div id=fb-root></div> <script>(function(d,
I have an application where users can store items. Now each type of account
I have an application where users select their own display columns. Each display column
I have an application with Users, Machines, and Tests. Users have many Machines and
In my web application users have the ability to upload games which are then
I have an application where multiple users can login(not simultaneously though!only a single user
I have a silverlight application which users will be running in various time zones
I have an application that allow users to choose picture from native gallery then

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.