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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:56:36+00:00 2026-05-25T18:56:36+00:00

I’ve built a simple Friend model, which allows Users to have multiple friends. Here’s

  • 0

I’ve built a simple Friend model, which allows Users to have multiple friends. Here’s what that looks like:

class Friend < ActiveRecord::Base
  belongs_to :user

class User < ActiveRecord::Base
  has_many :friends

Each friend record just has an id, user_id and friend_id. The user_id is the id of the user it belongs to and the friend_id is the id of user they are befriending.

Here’s my problem
I’m not quite sure how to display a list of a particular user’s friends. @user.friends will give me a list of all the friend records they have, but not the user accounts of those friends.

For instance, I am trying to build a show page for the friends controller:

class FriendsController < ApplicationController
def show
  @user = current_user
end

SHOW.HTML.ERB

<% if @user.friends.count > 0 %>
  <% @user.friends.each do |friend| %>
    <div class="entry">
      <%= friend.username %>

This does not work because friend in this case does not have username. I need to do something like this in my controller:

@friend = User.find_by_id(friend.friend_id)

But I’m not sure how I would call that in my view in the @user.friends loop. Any thoughts appreciated. Let me know if I need to be more clear.

UPDATE
I’ve updated my User model like so:

has_many :friends, :include => :user
has_many :friended_users, :through => :friends, :source => :user, :uniq => true

However, when I run @user.friended_users it’s giving me the user_ids (which is the same as @user) rather than friend_ids.

How can I tweak that relationship so it’s linking to the friend_id rather than user_id?

The more I think about it, I think I may not have set up the relationship properly in the first place. Maybe a User should has_many :users, through => 'friends', but that doesn’t really make sense…

UPDATE
I’ve updated my models based on @twooface’s input:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, :through => :friendships

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => 'User'

class Friend < ActiveRecord::Base
  has_many :friendships
  has_many :users

I’m just not sure what my Friends table should look like. I assume it should have a primary key and a user_id? If I create a friendship and friend record, I can do friendship.user and friendship.friend and get the correct results, but user.friends gives me an empty hash…

  • 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-25T18:56:36+00:00Added an answer on May 25, 2026 at 6:56 pm

    I think your relations are built a bit wrong. Try something like this:

    class User < ActiveRecord::Base
      has_many :friends
      has_many :friendships
      has_many :friends, :through => :friendships
    
    class Friendship < ActiveRecord::Base
      belongs_to :user
      belongs_to :friend, :class_name => 'User'
      # This class has :user_id and :friend_id
    

    Then everything will be simpler. Every user will have an array of friends that will be just users.

    User.first.friends
    

    Will return an array of Users that this User friends.

    Hope this helps.

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

Sidebar

Related Questions

I've got a string that has curly quotes in it. I'd like to replace
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Seemingly simple, but I cannot find anything relevant on the web. What is the
this is what i have right now Drawing an RSS feed into the php,
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.