title: Ruby error NoMethodError for nil:NilClass
body: <p>I'm following the Treebook tutorial on teamtreehouse.com (making a facebook clone) using rails. I have created a userfriendships controller and am getting this error when trying to reference a friend's full name in my index.html page of the friendships view: <strong>undefined method `full_name' for nil:NilClass</strong>. The problem occurs on line 4 here, (this is /views/user_friendships/index.html)</p>
  
  <pre><code>&lt;div class="page-header"&gt;
    &lt;h1&gt;Friends&lt;/h1&gt;
  &lt;/div&gt;
  &lt;hr /&gt;
  &lt;% if @user_friendships.empty? %&gt;
  &lt;em&gt;No &lt;%= params[:list] %&gt; friends yet!&lt;/em&gt;
  &lt;% end %&gt;
  
  &lt;% @user_friendships.each do |friendship| %&gt;
   &lt;% friend = friendship.friend %&gt;
   &lt;div id="&lt;%= dom_id(friendship) %&gt;" class="friend panel"&gt;
      &lt;div class="panel-heading"&gt;
        &lt;span class="pull-right label&gt;"&gt;&lt;%= "#{friendship.state}".upcase %&gt;&lt;/span&gt;
        &lt;h3 class="panel-title"&gt;&lt;%= friend.full_name %&gt;&lt;/h3&gt;
      &lt;/div&gt;
      &lt;div class="panel-body"&gt;
        &lt;div class="row"&gt;
          &lt;div class="col-md-1"&gt;Put gravatar url here&lt;/div&gt;
            &lt;div class="col-md-11"&gt;
              &lt;em&gt;Friends since time_ago_in_words ago&lt;/em&gt;
            &lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="panel-footer clearfix"&gt;
          &lt;%= link_to "Update friendship", edit_user_friendship_path( friend.profile_name), class: 'btn btn-primary pull-right' %&gt;
      &lt;/div&gt;
   &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>Here's the User Friendships Controller:</p>
  
  <pre><code>class UserFriendshipsController &lt; ApplicationController
  before_filter :authenticate_user!
  
    def index
      @user_friendships = current_user.user_friendships.all
  
    end
  
    def new
      if params[:friend_id]
        @friend = User.where(profile_name: params[:friend_id]).first
        raise ActiveRecord::RecordNotFound if @friend.nil?
        @user_friendship = current_user.user_friendships.new(friend: @friend)
      else
        flash[:error] = "Friend required"
      end
      rescue ActiveRecord::RecordNotFound 
        render file: 'public/404', status: :not_found
  
    end
  
    def create
  
      if params[:user_friendship] &amp;&amp; params[:user_friendship].has_key?(:friend_id)
        @friend = User.where(profile_name: params[:user_friendship][:friend_id]).first
        @user_friendship = UserFriendship.request(current_user, @friend)
        respond_to do |format|
          if @user_friendship.new_record?
            format.html do
              flash[:error] = "There was a problem creating that friend request."
              redirect_to profile_path(@friend)
            end
            format.json { render json: @user_friendship.to_json, status: :precondition_failed }
          else
            format.html do
              flash[:success] = "Friend request sent to #{@friend.full_name}."
              redirect_to profile_path(@friend.profile_name)
            end
            format.json { render json: @user_friendship.to_json }
          end
        end
      else
        flash[:error] = "Friend required"
        redirect_to root_path
      end
    end
  
    def edit
      @friend =  User.where(profile_name: params[:id]).first
      @user_friendship = current_user.user_friendships.find(params[:id])
    end
  
    def destroy
      @user_friendship = current_user.user_friendships.find(params[:id])
      if @user_friendship.destroy
        flash[:success] = "Friendship destroyed."
      end
      redirect_to user_friendships_path
    end
  
    def accept
      @user_friendship = current_user.user_friendships.find(params[:id])
      if @user_friendship.accept!
        flash[:success] = "You are now friend with #{@user_friendship.friend.full_name}"
      else
        flash[:error] = "That friendship could not be accepted."
      end
      redirect_to user_friendships_path
    end
  
    def block
      @user_friendship = current_user.user_friendships.find(params[:id])
      if @user_friendship.block!
        flash[:success] = "You have blocked #{@user_friendship.friend.full_name}."
      else
        flash[:error] = "That friendship could not be blocked."
      end
      redirect_to user_friendships_path
    end
  
    private
    def friendship_association
      case params[:list]
      when nil
        current_user.user_friendships
      when 'blocked'
        current_user.blocked_user_friendships
      when 'pending'
        current_user.pending_user_friendships
      when 'requested'
        current_user.requested_user_friendships
      when 'accepted'
        current_user.accepted_user_friendships
      end
    end
  
    private
    def user_friendship_attributes
      params.require(:user_friendship).permit(:user, :friend, :user_id, :friend_id, :state)
    end
  
  end
  </code></pre>
  
  <p>Lastly, I will include the user friendships model:</p>
  
  <pre><code>class UserFriendship &lt; ActiveRecord::Base
      belongs_to :user
      belongs_to :friend, class_name: 'User', foreign_key: 'friend_id'
  
      state_machine :state, initial: :pending do
          after_transition on: :accept, do: :send_acceptance_email
          state :requested
  
          event :accept do
              transition any =&gt; :accepted
          end
      end
  
      def self.request(user1, user2)
        transaction do
          friendship1 = create(user: user1, friend: user2, state: 'pending')
          friendship2 = create(user: user2, friend: user1, state: 'requested')
  
          friendship1.send_request_email
          friendship1
        end
      end
  
  
      def send_request_email
          UserNotifier.friend_requested(id).deliver
      end
  
      def send_acceptance_email
          UserNotifier.friend_request_accepted(id).deliver
      end
  
      def mutual_friendship
          self.class.where({user_id: friend_id, friend_id: user_id}).first
      end
  # Manually updating the state to avoid callbacks and infinite loops
      def accept_mutual_friendship
          mutual_friendship.update_attribute(:state, 'accepted')
      end
  end
  </code></pre>
  
  <p>I'm aware that I must have to change this somehow, but am at a loss as to how to do that. I would like this page to display all of the friends that a user has, either in an accepted or pending state. Can anyone see what I need to do?</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: If I change the views/user_friendships/index.html to <strong>friend.full_name</strong> instead of <strong>@friend.full_name</strong> I still get the same error. I can verify that the users currently in the db all have a valid first and last name (so the full name method should work).</p>
  
  <p>Another EDIT: Here is a rails console entry showing that I do have userfriendships in the database:</p>
  
  <pre><code>2.1.2 :003 &gt; UserFriendship.all
    UserFriendship Load (0.3ms)  SELECT "user_friendships".* FROM "user_friendships"
   =&gt; #&lt;ActiveRecord::Relation [#&lt;UserFriendship id: 1, user_id: 6, friend_id: nil, created_at: "2014-10-04 14:20:42", updated_at: "2014-10-04 14:20:42", state: nil&gt;, #&lt;UserFriendship id: 2, user_id: 6, friend_id: nil, created_at: "2014-10-04 14:21:32", updated_at: "2014-10-04 14:21:32", state: nil&gt;, #&lt;UserFriendship id: 3, user_id: 6, friend_id: 3, created_at: "2014-10-04 14:21:50", updated_at: "2014-10-04 14:21:50", state: nil&gt;, #&lt;UserFriendship id: 4, user_id: 6, friend_id: 6, created_at: "2014-10-04 14:23:58", updated_at: "2014-10-04 14:23:58", state: nil&gt;, #&lt;UserFriendship id: 5, user_id: 6, friend_id: 6, created_at: "2014-10-04 14:25:05", updated_at: "2014-10-04 14:25:05", state: nil&gt;, #&lt;UserFriendship id: 6, user_id: 6, friend_id: 4, created_at: "2014-10-04 14:25:15", updated_at: "2014-10-04 14:25:15", state: nil&gt;, #&lt;UserFriendship id: 7, user_id: 6, friend_id: 6, created_at: "2014-10-04 20:42:40", updated_at: "2014-10-04 20:42:40", state: nil&gt;, #&lt;UserFriendship id: 8, user_id: 6, friend_id: 6, created_at: "2014-10-04 20:45:44", updated_at: "2014-10-04 20:45:44", state: nil&gt;, #&lt;UserFriendship id: 9, user_id: 6, friend_id: 6, created_at: "2014-10-04 21:38:17", updated_at: "2014-10-04 21:38:17", state: nil&gt;, #&lt;UserFriendship id: 10, user_id: 6, friend_id: 6, created_at: "2014-10-09 16:53:44", updated_at: "2014-10-09 16:53:44", state: "pending"&gt;, ...]&gt;
  2.1.2 :004 &gt;
  </code></pre>
  
  <p>Another Edit: Here is the user model models/user.rb:</p>
  
  <pre><code>class User &lt; ActiveRecord::Base
    # Include default devise modules. Others available are:
    # :confirmable, :lockable, :timeoutable and :omniauthable
    devise :database_authenticatable, :registerable,
           :recoverable, :rememberable, :trackable, :validatable
  
      validates :first_name, presence: true
  
      validates :last_name, presence: true
  
      validates :profile_name, presence: true, 
                               uniqueness: true,
                               format: {
                                  with: /\A.+\z/,
                                  message: "must be formatted correctly"
                               }
  
      has_many :statuses
      has_many :user_friendships
      has_many :friends, -&gt; { where user_friendships: { state: 'accepted'} }, through: :user_friendships
      has_many :pending_user_friendships, -&gt; { where state: 'pending' },
                                        class_name: 'UserFriendship', 
                                        foreign_key: :user_id
      has_many :pending_friends, through: :pending_user_friendships, source: :friend
  
      def full_name
          first_name + " " + last_name
      end
  
      def gravatar_url
          stripped_email = email.strip
          downcased_email = stripped_email.downcase
          hash = Digest::MD5.hexdigest(downcased_email)
          "http://gravatar.com/avatar/#{hash}"
  
      end
  end
  </code></pre>