title: Rails - Unknown attribute password
body: <p>I have been following Michael Hartl's Ruby on Rails tutorial book to try and add users to my application. Reading chapter 6, I have added what I believe to be the necessary fields for my user, specifically password and password confirmation via "has_secure_password". </p>
  
  <p>I thought that adding "has_secure_password" to my user model would include the attributes "password" and "password_confirmation" provided I add a "password_digest" to the model. I have done that as the book instructed me to. However, when I run a test, Rails gives me the following error:</p>
  
  <pre><code>Error:
  UserTest#test_should_be_valid:
  ActiveModel::UnknownAttributeError: unknown attribute 'password' for User.
      test/models/user_test.rb:8:in `setup'
  </code></pre>
  
  <p>I tried <a href="https://stackoverflow.com/questions/12142374/railstutorial-org-chapter-6-unknown-attribute-password">this</a> solution and it still gave me the same error, not recognizing the attributes "password" or "password_confirmation". I installed bcrypt using "gem install bcrypt" and included the following in my gem file:</p>
  
  <blockquote>
    <p>gem 'bcrypt-ruby', :require => 'bcrypt'</p>
  </blockquote>
  
  <p>I am using Rails 5 and it seems like "has_secure_password" is not supplying the password attributes that I need. Can anyone see what I missed or did wrong that caused "has_secure_password" to not work as intended? Thanks</p>
  
  <p>User Model:</p>
  
  <pre><code>class User &lt; ApplicationRecord
  
    has_many :activities
  
    class User &lt; ActiveRecord::Base
  
      attr_accessor :name, :email, :password, :password_confirmation
      has_secure_password
  
      validates :first_name, presence: true, length: {minimum: 1}
      validates :last_name, presence: true, length: {minimum: 1}
      validates :email, presence: true, uniqueness: true, length: {minimum: 5}
      validates :username, presence: true, uniqueness: true, length: {minimum: 1}
      validates :password_digest, length: {minimum: 6}
      validates :password, :confirmation =&gt; true, length: {minimum: 4}
      validates :password_confirmation, presence: true
  
      #-----------------------New Stuff ---------------------------------------
      acts_as_authentic do |c|
        c.crypto_provider = Authlogic::CryptoProviders::Sha512
      end
      #------------------------------------------------------------------------
  
      #---------------Unsure if working--------------
      #validates_presence_of :password, :on =&gt; :create
      #validates_presence_of :email
      #validates_uniqueness_of :email
      #----------------------------------------------
  
      def self.authenticate(email, password)
        user = find_by_email(email)
        if user &amp;&amp; user.password_hash == BCrypt::Engine.hash_secret(password, user.password_salt)
          user
        else
          nil
        end
      end
  
      def encrypt_password
        if password.present?
          self.password_salt = BCrypt::Engine.generate_salt
          self.password_hash = BCrypt::Engine.hash_secret(password, password_salt)
        end
      end
    end
  end
  </code></pre>
  
  <p>Apologies for the messy code on the model as I am still learning Rails.</p>
  
  <p>User Controller:</p>
  
  <pre><code>class UsersController &lt; ApplicationController
    def new
      @user = User.new
    end
  
    def create
      @user = User.new(user_params)
      if @user.save
        flash[:success] = 'Account created'
      else
        flash[:notice] ='ERROR: Account was not created'
        redirect_to 'users/new'
      end
    end
  
    def show
      @user = User.find(params[:id])
    end
  
  
    private
    def user_params
      params.require(:user).permit(:first_name, :last_name, :email, :password, :password_confirmation)
    end
  end
  </code></pre>
  
  <p>User Table:</p>
  
  <pre><code> create_table "users", force: :cascade do |t|
      t.string   "first_name"
      t.string   "last_name"
      t.string   "username"
      t.string   "email"
      t.datetime "created_at",        null: false
      t.datetime "updated_at",        null: false
      t.string   "persistence_token"
      t.string   "password_digest"
      t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    end
  </code></pre>
  
  <p>User Test:</p>
  
  <pre><code>require 'test_helper'
  
  class UserTest &lt; ActiveSupport::TestCase
    # test "the truth" do
    #   assert true
    # end
    def setup
      @user = User.new(first_name: 'test', last_name: 'tester', password: '1234',email: 'test1@mail.com',
                       password: 'foobar', password_confirmation: 'foobar')
    end
    test 'should be valid' do
      assert @user.valid?
    end
  end
  </code></pre>