0

I use the command like this:

administrator@ubuntu:~/demo$ rails generate migration Testabc test123:string

and the terminal respond:

invoke active_record
create db/migrate/20120204124219_testabc.rb

But in the file`s content is:

class Testabc < ActiveRecord::Migration
  def up
  end

  def down
  end
end

So, where is my table and the string column?

tshepang
  • 12,111
  • 21
  • 91
  • 136

2 Answers2

3

Your migration needs to look like rails g migration add_<field>_to_<table> field:type. For example, this:

rails generate migration add_fieldname_to_tablename fieldname:string

will produce this:

class AddFieldnameToTablename < ActiveRecord::Migration
  def up
    add_column :tablenames, :fieldname, :string
  end

  def down
    remove_column :tablenames, :fieldname
  end
end

If you wanted to generate a model from scratch, then you should do:

rails generate model Widget fieldname:string

which will produce a migration that includes the fields needed for the model.

John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • At first,I just want to create a middle table to connect two tables,build a many-to-many relationship. This is just a simple example,I do not intending to modify a table :) – Rinko Kobayakawa Feb 04 '12 at 13:52
0

Maybe you're looking for:

rails generate model Testabc test123:string
gtd
  • 16,956
  • 6
  • 49
  • 65