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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:39:10+00:00 2026-06-01T09:39:10+00:00

I’ve set up a self-referencing relationship model for users to be friends with one

  • 0

I’ve set up a self-referencing relationship model for users to be friends with one another. I am able to create a friend request (two entries in my DB, one for the friendship and one for the inverse friendship) without any problems.

I am having trouble declining a friend request though. I keep getting this error when I press decline which takes me to the destroy action:

NoMethodError in FriendshipsController#destroy
undefined method `destroy' for nil:NilClass

One of the database records, pending friendship, is destroyed even though I get the error above, and if I comment out @friendship2 = @user.friendships.find_by_friend_id(params[:id]).destroy, the other record gets destroyed too. So it looks like both line are work individually, but returns the above error.

SOLUTION: It looks like my two destroy statements were referencing the same record. I’ve commented out the wrong code in my controller and added the ones that work for me.

Here is my Friendships controller:

class FriendshipsController < ApplicationController
  before_filter :authenticate, :only => [:update, :create, :destroy]

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

  def destroy
    @user = User.find(params[:user_id])
    @friend = User.find(params[:id])
    #@friendship2 = @user.friendships.find_by_friend_id(params[:id]).destroy
    #@friendship1 = @friend.friendships.find_by_id(params[:user_id]).destroy  
    @friendship1 = @user.friendships.find_by_friend_id(@friend.id).destroy #removes the requested friendship
    @friendship2 = @friend.friendships.find_by_friend_id(@user.id).destroy #removes the pending friendship

    flash[:success] = "Removed."
    redirect_to @user   
  end 
end

Here is my User model:

class User < ActiveRecord::Base
  has_many :friendships, :dependent => :destroy

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

  has_many :pending_friends, 
     :through => :friendships, 
     :conditions => "status = 'pending'", 
     :foreign_key => "user_id", 
     :source => :friend

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

Here is my view (users/show.html.erb):

<% if signed_in? && @user == current_user %>
  <% unless current_user.pending_friends.empty? %>
    <h2>Pending</h2>
    <% current_user.pending_friends.each do |pending| %>
       <%= pending.name %>
       <%= link_to '[Accept]', friendship_path(:user_id => current_user, :id => pending), :method => :put, :confirm => "Accept?" %>
       <%= link_to '[Decline]', friendship_path(:user_id => current_user, :id => pending), :method => :delete, :confirm => "Decline?" %>
    <% end %>
  <% end %>
<% end %>

I have two questions.

  1. I’ve seen from some tutorials/discussion about the use of inverse_friendships, but is this really necessary? So far, I haven’t seen the need for this, but then again, I’ve only gotten as far as coding my create and destroy action.

  2. What’s up with the undefined method ‘destroy’ error?

Thanks for getting through this wall of text! 😀

  • 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-01T09:39:11+00:00Added an answer on June 1, 2026 at 9:39 am

    Regarding the first one, it’s up to you and your system. Creating different variable to hold every type of friendship might be a bit hardcore though, but it gives you full information, more control and there’s no data duplication in the database.

    Regarding the second one you should check whether your query return a result. Since your exception:

    undefined method `destroy’ for nil:NilClass

    Says there’s no method “destroy” in NilClass (not friendship). Just check the :id parameter and whether find_by_friend_id returns anything at all.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.