title: Undefined method 'destroy' when attempting to destroy/decline friendship relationship
body: <p>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.</p>
  
  <p>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:</p>
  
  <pre><code>NoMethodError in FriendshipsController#destroy
  undefined method `destroy' for nil:NilClass
  </code></pre>
  
  <p>One of the database records, pending friendship, is destroyed even though I get the error above, and if I comment out <code>@friendship2 = @user.friendships.find_by_friend_id(params[:id]).destroy</code>, the other record gets destroyed too. So it looks like both line are work individually, but returns the above error.</p>
  
  <p>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.</p>
  
  <p>Here is my Friendships controller:</p>
  
  <pre><code>class FriendshipsController &lt; ApplicationController
    before_filter :authenticate, :only =&gt; [:update, :create, :destroy]
  
  def create
    @user = User.find(current_user)
    @friend = User.find(params[:friend_id])
    params[:friendship1] = {:user_id =&gt; @user.id, :friend_id =&gt; @friend.id, :status =&gt; 'requested'}
    params[:friendship2] = {:user_id =&gt; @friend.id, :friend_id =&gt; @user.id, :status =&gt; '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
  </code></pre>
  
  <p>Here is my User model: </p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    has_many :friendships, :dependent =&gt; :destroy
  
    has_many :friends, 
       :through =&gt; :friendships, 
       :conditions =&gt; "status = 'accepted'", 
       :source =&gt; :friend 
  
    has_many :pending_friends, 
       :through =&gt; :friendships, 
       :conditions =&gt; "status = 'pending'", 
       :foreign_key =&gt; "user_id", 
       :source =&gt; :friend
  
    has_many :requested_friends, 
       :through =&gt; :friendships,
       :source =&gt; :friend, 
       :conditions =&gt; "status = 'requested'"    
  end
  </code></pre>
  
  <p>Here is my view (users/show.html.erb):</p>
  
  <pre><code>&lt;% if signed_in? &amp;&amp; @user == current_user %&gt;
    &lt;% unless current_user.pending_friends.empty? %&gt;
      &lt;h2&gt;Pending&lt;/h2&gt;
      &lt;% current_user.pending_friends.each do |pending| %&gt;
         &lt;%= pending.name %&gt;
         &lt;%= link_to '[Accept]', friendship_path(:user_id =&gt; current_user, :id =&gt; pending), :method =&gt; :put, :confirm =&gt; "Accept?" %&gt;
         &lt;%= link_to '[Decline]', friendship_path(:user_id =&gt; current_user, :id =&gt; pending), :method =&gt; :delete, :confirm =&gt; "Decline?" %&gt;
      &lt;% end %&gt;
    &lt;% end %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>I have two questions.</p>
  
  <ol>
  <li><p>I've seen from some tutorials/discussion about <a href="http://iterat.ive.ly/index.php/2010/08/31/a-user-friend-relationship-model-in-rails/" rel="nofollow">the use of inverse_friendships</a>, 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.</p></li>
  <li><p>What's up with the undefined method 'destroy' error?</p></li>
  </ol>
  
  <p>Thanks for getting through this wall of text! :D</p>