Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails generate migration not working

I've got below command run on console

rails g migration payslips first_name:string last_name:string

But this just generates empty file like below

class Payslips < ActiveRecord::Migration
  def change
  end
end

I cannot find the reason why. Is there something wrong with the console generate command?

like image 302
Passionate Engineer Avatar asked Oct 01 '13 03:10

Passionate Engineer


3 Answers

Is correct functionality, if you want create a model then you need run:

rails g model payslips first_name:string last_name:string

Then you get:

class CreatePayslips < ActiveRecord::Migration
  def change
    create_table :payslips do |t|
      t.string :first_name
      t.string :last_name

      t.timestamps
    end
  end
end
like image 124
efrenfuentes Avatar answered Oct 23 '22 12:10

efrenfuentes


Use the word: Create before your table name.

$ rails generate migration CreateProducts name:string part_number:string

generates:

class CreateProducts < ActiveRecord::Migration
  def change
    create_table :products do |t|
      t.string :name
      t.string :part_number
    end
  end
end

source: http://guides.rubyonrails.org/migrations.html

like image 3
DoctorRu Avatar answered Oct 23 '22 14:10

DoctorRu


Assuming that your model has been created, you must be more explicit when explaining what you really want to do :

rails g migration add_first_name_and_last_name_to_payslips first_name:string last_name:string

The above tells the migrator to add first_name and last_name to the payslips table, so you end up with this migration :

class AddFirstNameAndLastNameToPayslips < ActiveRecord::Migration
  def change
    add_column :payslips, :first_name, :string
    add_column :payslips, :last_name, :string
  end
end
like image 2
kirkytullins Avatar answered Oct 23 '22 12:10

kirkytullins