Skip to content

05b User Model Validation

Dave Strus edited this page Jul 18, 2015 · 1 revision

Solution

bin/rails g migration create_users
class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
      t.string :username
      t.string :name
      t.string :password_digest
      t.timestamps
    end
  end
end
class User < ActiveRecord::Base
  has_secure_password
end

Extra

Add a validation to make sure the password is at least 8 characters.

class User < ActiveRecord::Base
  has_secure_password
  validates :password, length: { minimum: 8 }
end

Create the users table.

Migrate the database to create the new table.

bin/rake db:migrate