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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:56:04+00:00 2026-06-14T21:56:04+00:00

I have a relationship table for users and related users. If I do @user.related_users

  • 0

I have a relationship table for users and related users.

If I do @user.related_users I get a list of user_id that point to the users.

On my view I want to list their names with

<% current_user.related_users.each do |u| %>
<%= User.find(u).name %>
<% end %>

I worry that it might overload the DB too much, since it fetches every related_user from the DB every time someone access that view.

Is there a better way of doing this? or that is the way and there’s nothing I can do about it?

  • 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-14T21:56:05+00:00Added an answer on June 14, 2026 at 9:56 pm

    That’s what eager loading is for. Ideally, in your model, related_users should be an association:

    has_many :related_users # add conditions to find the related users if necessary
    

    Then in your controller, fetch the @user like this:

    @user = User.includes(:related_users)
    

    That will perform only 1 query for all related users, instead of one query for each related user (a so-called 1+N problem).

    Or, if you don’t want to rewrite your model code, simply load all related users into a variable in your controller:

    @related_user = User.where(id: current_user.related_users)
    

    and iterate over that in your view. This will fetch all related users with a single query with a WHERE id IN (...) clause.

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

Sidebar

Related Questions

I have two tables that need to be related--a users table and an address_book
In an app, Users and Cases have a many-to-many relationship. Users pull their list
I have a many-to-many relationship between users and groups and I have a table
Is it possible to have a relationship from a user table to a system
I have 2 tables: users and subscribers . The relationship of the user to
I currently have a user's table which contains a one-to-one relationship for Youtube OAuth
I have a user table with a self-referential relationship between instructors and students. Currently
I have a table Product. Product is related to ProductDescription in a one-to-many relationship.
I have several models that are related to one another through a HABTM relationship.
I have 4 models I'm working with: User, List, Permission, Invitation. They are related

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.