title: Rails 4 - undefined method 'attributes' for nil:NilClass
body: <p>I have a many-to-many relationship between Users and Documents. Users can invite other users to collaborate on their documents by entering their email. The application will look up a user by email and set the user_id in DocumentUser. If a user is not found, I am getting this error: Rails 4 - undefined method 'attributes' for nil:NilClass. This error occurs in DocumentUsersControlller.create. How can I prevent this error using best practices? Also, why doesn't the presence validator prevent this error?</p>
  
  <p>Here are the relevant controllers and models:</p>
  
  <pre><code>class DocumentUsersController &lt; ApplicationController
  
    def new
      @document = Document.find_by link_key: params[:document_id]
      @document_user = @document.document_users.build
    end
  
  def create
  
    @document = Document.find_by link_key: params[:document_id]
    @document_user = @document.document_users.build(document_user_params)
    user = User.find_by email: params[:document_user][:email]  
    @document_user.user = user if user
  
    respond_to do |format|
        if @document_user.save  #error occurs here if user is Nil
            format.html { redirect_to @document, notice: 'User Invitation was sent.'}
            format.json { render action: 'show', status: :created, location: @document_user }
        else
            format.html { render action: 'new' }
            format.json { render json: @document_user.errors, status: :unprocessable_entity }
        end
  
    end
  end
  
  def destroy
  end
  
  private
  
  def document_user_params
      params.require(:document_user).permit(:email, :admin, :editor, :viewer)
  end
  
  end
  
  
  class DocumentUser &lt; ActiveRecord::Base
    include KeyCreator
  
    belongs_to :document
    belongs_to :user
    before_create :before_create
  
      attr_accessor :email
  
      validates :user, uniqueness: { message: "has already been invited" }
      validates :user, presence: true
      validate :email_matches_user
  
      def to_param
          "#{invite_key}".parameterize
      end
  
   private
  
      def before_create
          now = DateTime.now      
          self.added_date = now
          self.modified_date = now
          self.invite_key = create_key
          self.api_key = create_key
      end
  
     def email_matches_user
         unless User.exists? email: email 
             errors.add(:email, "does not match any existing user")
         end
     end
  
  end
  
  
  class User &lt; ActiveRecord::Base
      include KeyCreator
  
      # Include default devise modules. Others available are:
      # :confirmable, :lockable, :timeoutable and :omniauthable
      devise :database_authenticatable, :registerable, :confirmable, 
       :recoverable, :rememberable, :trackable, :validatable
  
  has_many :document_users
  has_many :documents, :through =&gt; :document_users
  before_create :before_create
  
  private
  
      def before_create
          self.api_key = create_key
      end
  
  end
  
  
  class Document &lt; ActiveRecord::Base
       include KeyCreator
  
  has_many :document_users, dependent: :destroy
      has_many :users, :through =&gt; :document_users
      before_create :before_create
  
      def self.created_by(user)
          Document.joins(:document_users).where(:document_users =&gt; {:user =&gt; user, :creator =&gt; true}).order(:name)
      end
  
      def self.guest_in(user)
          Document.joins(:document_users).where(:document_users =&gt; {:user =&gt; user, :creator =&gt; false}).order(:name)
      end
  
      def to_param
          "#{link_key}".parameterize
      end
  
  private
  
      def before_create
          now = DateTime.now
          self.content = nil
          self.created_date = now
         self.modified_date = now
         self.deleted_date = nil
         self.api_key = create_key
         self.link_key = create_key
      end
  
  end
  </code></pre>