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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:27:01+00:00 2026-05-27T15:27:01+00:00

PROBLEM: @user.friends does not work. It’s returning 2 records and it should be 4..

  • 0

PROBLEM: @user.friends does not work. It’s returning 2 records and it should be 4..

I have the following models:

class User < ActiveRecord::Base
  has_many :friendships
  has_many :friends, 
    :through => :friendships,
    :conditions => "status = 'accepted'", 
    :order => :fname
  has_many :requested_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'requested'"
  has_many :pending_friends, 
    :through => :friendships, 
    :source => :friend,
    :conditions => "status = 'pending'"

class Friendship < ActiveRecord::Base
  belongs_to :user
  belongs_to :friend, :class_name => "User", :foreign_key => "friend_id"

For some reason. <%=@user.friends%> is not returning all of the user’s friends.

Example data:

> @user.friendships.all.length
=> 4
> @user.friendships
=> [#<Friendship id: 20, user_id: 11, friend_id: 20, status: "accepted", created_at: "2011-12-22 12:59:22", updated_at: "2011-12-22 17:02:54">, #<Friendship id: 8, user_id: 11, friend_id: 12, status: "accepted", created_at: "2011-12-22 06:29:02", updated_at: "2011-12-22 07:41:24">, #<Friendship id: 3, user_id: 11, friend_id: 1, status: "approved", created_at: "2011-12-22 05:48:29", updated_at: "2011-12-22 06:22:09">, #<Friendship id: 1, user_id: 11, friend_id: 641, status: "approved", created_at: "2011-12-22 04:47:19", updated_at: "2011-12-22 04:47:19">]
> @user.friends.length
=> 2

@user.friends.length should have been 4 as the data above shows all the statuses as “accepted”. Any idea where I messed up in the model associations listed above?

Thank you!

  • 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-27T15:27:02+00:00Added an answer on May 27, 2026 at 3:27 pm

    status is “approved” for two of the friendships and “accepted” for two. That’s why it is returning only two accepted friendships as per your condition.

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

Sidebar

Related Questions

I have a bit of a problem with user controls. Basically what I want
My problem is that I have a user that is having a problem displaying
I have a problem with the MySQL root user in My MySQL setup, and
I have a problem regarding getting the path of a user control. The scenario
I'm trying to run the following spec: describe UsersController, GET friends do it should
I have 2 models.py files in different app directories: users.models.py and friends.models.py. There is
Example Problem: Entities: User contains name and a list of friends (User references) Blog
I'm using the following code to retrieve the user's friends list and the info
I have the following entities: User Company Organization Users need to be able to
I have some troubles when using javascript code for Facebook invite friends. Details: User

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.