Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Friendship model, how to determine if a friendship model already exists between 2 users

I have a Friend model:

user_id, friend_id, status (approved, pending, ignored)

Given a user currently logged in, I want to be able to do something like

current_user.friendship_exists(@user)

Something to tell if a friendship exists for a logged in user looking at another user. To prevent either user from seeing "add friend" again when the record already exists.

Ideally I could show the user who created the friendship "Friend Request Sent"

And the user who needs to take action "respond to friendship request" where I can then so a approve & reject option.

Ideas on how I can do this? Show a button for either

  • "Add Friend"
  • "Friend Request Sent"
  • "Respond to Friend Request"
like image 542
AnApprentice Avatar asked Nov 27 '25 15:11

AnApprentice


1 Answers

If you're just looking for the friendship_exists method, this is already built into Rails (assuming your User model has many friends):

current_user.friends.exists?(@user)
like image 78
Peter Brown Avatar answered Nov 29 '25 05:11

Peter Brown



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!