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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:14:04+00:00 2026-06-01T02:14:04+00:00

I am trying to hide my Add friend link if – current_user and @user

  • 0

I am trying to hide my “Add friend” link if
– current_user and @user are friends
– there is a pending friend request
– current_user and @user have the same id (same user)

The “Add friend” link does not show if current_user and @user are friends. The problem is when there is a pending friend request. I still see the “Add friend” link after I submit a friend request.

Is there something wrong with the conditions in my view?

When I run these two conditions in my console:
current_user.pending_friends.include?(@user) and @user.pending_friends.include?(current_user), I get a false even though my database shows a pending request.

Here is my view:

users/show.html.erb

<% if signed_in? %>
  <% if @user == current_user || current_user.friends.include?(@user) 
        || current_user.pending_friends.include?(@user) 
        || @user.pending_friends.include?(current_user)  %>


   <% else %>      

    <%= link_to  friendships_path(:user_id => current_user.id, 
            :friend_id => @user.id), :method => :post, :action => 'create' do %>

    Add Friend

    <% end %>
  <% end %>
<% end %>

User Model

has_many :friendships

has_many :inverse_friendships, 
 :class_name => "Friendship", 
 :foreign_key => "friend_id"

has_many :direct_friends, 
 :through => :friendships, 
 :conditions => "status = 'accepted'", 
 :source => :friend

has_many :inverse_friends, 
 :through => :inverse_friendships, 
 :conditions => "status = 'accepted'", 
 :source => :user

has_many :pending_friends, 
 :through => :friendships, 
 :conditions => "status = 'pending'", 
 :foreign_key => "user_id", 
 :source => :user   #this line is incorrect. see correction below.

has_many :requested_friends, 
 :through => :friendships,
 :source => :friend, 
 :conditions => "status = 'requested'"

def friends
  direct_friends | inverse_friends
end

Here is the create action from my friendships controller so you guys can see how the relationship is being created in the db:

def create
  @user = User.find(current_user)
  @friend = User.find(params[:friend_id])
  params[:friendship1] = {:user_id => @user.id, :friend_id => @friend.id, :status => 'pending'}
  params[:friendship2] = {:user_id => @friend.id, :friend_id =>@user.id, :status => 'requested'}
  @friendship1 = Friendship.create(params[:friendship1])
  @friendship2 = Friendship.create(params[:friendship2])

  if @friendship1.save && @friendship2.save
    flash[:success] = "Friend request submitted."
    redirect_to @friend
  else 
    flash[:error] = "Friend request failed."
    redirect_to @friend
  end  
end

Thanks for looking!

Edit: Found my mistake and posted the answer below.

The source for :pending_friends should be :friend, not :user. Correction below.

has_many :pending_friends, 
:through => :friendships, 
:conditions => "status = 'pending'", 
:foreign_key => "user_id", 
:source => :friend
  • 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-01T02:14:06+00:00Added an answer on June 1, 2026 at 2:14 am

    I suggest you add the unit tests for those associations, since they look like complicated and not easy to understand.

    1. add fixtures:

      # test/fixtures/users.yml
      current_user: 
        id: 1
        name: Current User
      another_user:
        id: 2 
        name: Your friend
      
      # test/fixtures/friendships.yml
      pending_friend:
        id: 1
        status: pending
        user_id: 1 # 
        # other attributes
      requested_friend:
        id: 2
        status: requested
        user_id: 2 # 
        # other attributes
      
    2. write the unit test code to tell yourself that the associations are correct:

      # test/model/user_test.rb
      class UserTest <  # extend Rails unit test class
        def test_the_requested_friendship 
          assert users(:current_user).requested_friends.include?( users(:another_user))
        end
        def test_the_pending_friendships
          # your assertion here
        end
      end
      

    NOTICE: the code above is just an idea, I haven’t make them tested yet. you should implement your code yourself.

    and I suggest that you use “rspec” since it’s more expressive than the classic test-unit.

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

Sidebar

Related Questions

I am trying to hide some divs before the user prints this giant form,
I am trying to hide the button based on the user's role using the
I'm trying to hide window after its startup. I have own window-class which is
I'm trying to hide some of my Perl program from the end user to
Im trying to show/hide a div using jquery when a link gets clicked. I
I'm trying to add a blank space in the following. $('.post_name'+post_id+'').hide(); So it will
I have a flash object that I am trying to show and hide along
I have a javascript function that I am trying to add to my document,
I have a working Ajax form, now I'm trying to add file upload to
im trying to hide inserted rows into a table that have the class newrow.

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.