Storing User Data

Before users can do any of that, though, Rails needs a table for storing their data. The generator created a migration file in db/migrate, with a name ending in create_users, shown in Example 14-1.

Example 14-1. The migration for creating users in the database

class CreateUsers < ActiveRecord::Migration
  def self.up
    create_table "users", :force => true do |t|
      t.column :login,                     :string
      t.column :email,                     :string
      t.column :crypted_password,          :string, :limit => 40
      t.column :salt,                      :string, :limit => 40
      t.column :created_at,                :datetime
      t.column :updated_at,                :datetime
      t.column :remember_token,            :string
      t.column :remember_token_expires_at, :datetime

    end
  end

  def self.down
    drop_table "users"
  end
end

There’s no pressing need to change this, but if you know you have plans for additional fields about your users, you could add additional columns. (This migration uses the older t.column syntax rather than the newer t.datatype syntax, but it should still be clear what’s being created here.) To create the table, run rake db:migrate.

Sessions don’t have a model or migrations—they’re just a controller that operates on users, so there isn’t anything more to do with them yet.

Get Learning Rails: Live Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.