O'Reilly logo

Learning Rails: Live Edition by Edd Dumbill, Simon St. Laurent

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required