title: Test complaining NoMethodError: undefined method `name' for nil:NilClass
body: <p>Just added <code>bcrypt</code> and a <code>password_digest</code> column to database. Now I am getting this error. It is saying it doesn't recognize name, this test was passing. Now it is failing. All other tests are passing ( including one of <code>password_digest</code>, oddly enough). I am using the  testing framework that comes with rails.</p>
  
  <pre><code>  def test_can_create_profiles
      user = create_user('tester@test.com', 'hassan') #creates a profile with randomized password
      assert_equal "hassan", User.find_by_email('tester@test.com').name
    end 
  </code></pre>
  
  <p>bundle exec rake test</p>
  
  <pre><code>  1) Error:
  UserTest#test_can_create_profiles:
  NoMethodError: undefined method `name' for nil:NilClass
      test/models/user_test.rb:9:in `test_can_create_profiles'
  
  
  
  class User &lt; ActiveRecord::Base
    email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    before_save {self.email = email.downcase}  
    validates :name, presence: true, length: { maximum: 20 }
    validates :email, :presence =&gt; true,
      :format =&gt; { :with =&gt; email_regex },
      :uniqueness =&gt; { :case_sensitive =&gt;
        false }
    has_secure_password
  end
  </code></pre>
  
  <p>If there is any information I am missing pelase let me know.</p>
  
  <hr>
  
  <pre><code>def create_user(email, name)
    user = User.new(name: name, email: email)
    user.password_digest = (0...8).map { (65 + rand(26)).chr }.join
    user.save
    user
  end
  </code></pre>
  
  <p>AGHH</p>
  
  <pre><code>x = User.new
  =&gt; #&lt;User id: nil, name: nil, email: nil, created_at: nil, updated_at: nil, password_digest: nil&gt;
  irb(main):002:0&gt; x.name ='lol'
  =&gt; "lol"
  irb(main):005:0&gt; x.email = 'apple@ape.com'
  =&gt; "apple@ape.com"
  
  irb(main):007:0&gt; x.password_digest = 'lol'
  =&gt; "lol"
  irb(main):008:0&gt; x.save
     (0.1ms)  begin transaction
    User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('apple@ape.com') LIMIT 1
     (0.1ms)  rollback transaction
  =&gt; false
  </code></pre>
  
  <hr>
  
  <p>Okay so the problem is somewhere over here!</p>
  
  <pre><code>irb(main):010:0&gt; raise x.errors.inspect
  RuntimeError: #&lt;ActiveModel::Errors:0x007f6fae5b3bf8 @base=#&lt;User id: nil, name: "lol", email: "apple@ape.com", created_at: nil, updated_at: nil, password_digest: "lol"&gt;, @messages={:password=&gt;["can't be blank"]}&gt;
  
  irb(main):013:0&gt; raise x.password= 'hello'
  RuntimeError: hello
      from (irb):13
      from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.1/lib/rails/commands/console.rb:90:in `start'
      from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.1/lib/rails/commands/console.rb:9:in `start'
      from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.1/lib/rails/commands.rb:62:in `&lt;top (required)&gt;'
      from bin/rails:4:in `require'
      from bin/rails:4:in `&lt;main&gt;'
  irb(main):014:0&gt; raise x.errors.inspect
  RuntimeError: #&lt;ActiveModel::Errors:0x007f6fae5b3bf8 @base=#&lt;User id: nil, name: "lol", email: "apple@ape.com", created_at: nil, updated_at: nil, password_digest: "$2a$10$CMPQdQSLKQTS5r0G2a1oheDhblzxcp65KxStZbKKAHmy..."&gt;, @messages={:password=&gt;["can't be blank"]}&gt;
      from (irb):14
      from /home/user/.rbenv/versions/2.0.0-p247/lib/ruby/gems/2.0.0/gems/railties-4.0.1
  </code></pre>
  
  <p>ugh : c</p>
  
  <p>Why is this happening?</p>
  
  <p>EDIT : bcrypt requires a password and password_confirmation field to be filled in.</p>
  
  <p>That was what was causing trouble, as well as a broken test. >.&lt;</p>