Don P
Don P

Reputation: 63738

What is the Rails Migration for creating a table?

What is the Rails Migration for creating a table?

I tried this migration generator:

$ rails g migration CreateQuestions user:references question_title:string question_text:text approved:boolean

This creates a migration that seems to be missing the usual id and timestamps fields. Did I mess up my generator?

class CreateQuestions < ActiveRecord::Migration
  def change
    create_table :questions do |t|
      t.references :user, index: true
      t.string :question_title
      t.text :question_text
      t.boolean :approved
    end
  end
end

Upvotes: 1

Views: 343

Answers (1)

Fellow Stranger
Fellow Stranger

Reputation: 34103

  1. "A primary key column called id will also be added implicitly, as it's the default primary key for all Active Record models."

  2. If you wanna add the timestamps, add this to your migration file:

    t.timestamps
    

Upvotes: 3

Related Questions