1

Trying to add first_name & last_name to an existing model User

First did this:

rails g migration first_name_to_users first_name:string

then did this:

rails g migration last_name_to_users last_name:string

obviously ran rake db:migrate which resulted in this:

== FirstNameToUsers: migrating =============================================== == FirstNameToUsers: migrated (0.0000s) ======================================

== LastNameToUsers: migrating ================================================ == LastNameToUsers: migrated (0.0000s) =======================================

But it doesn't show up in the table!

If I go into rails console and run User.column_names, I get this:

=> ["id", "email", "encrypted_password", "reset_password_token", "reset_password_sent_at", "remember_created_at", "sign_in_count", "current_sign_in_at", "last_sign_in_at", "current_sign_in_ip", "last_sign_in_ip", "created_at", "updated_at"] 

If anyone asks if it shows up in db/migrate folder, the answer is yes.

Here's the last_name one:

class LastNameToUsers < ActiveRecord::Migration
    def change
    end
end

So ... why is it not showing up in the table?

Surya
  • 15,703
  • 3
  • 51
  • 74
user273072545345
  • 1,536
  • 2
  • 27
  • 57

4 Answers4

4

Do this in one migration file,

rails g migration add_columns_to_users

Then in the migration file which is generated using the above command do this:-

class AddColumnsToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end
Rajdeep Singh
  • 17,621
  • 6
  • 53
  • 78
3

Firstly, the main reason you generate a migration with console command is to get the time stamp, you can feel free to alter the migration generated. Such as adding a line in the change function

  def change
    add_column :user, :first_name, :string
  end

and secondly if you want that to be automatically generated by console command, you need prefix with add_column, such as

rails g migration add_column_first_name_to_user first_name:string

Keep in mind that not all migrations can be (or should be) auto generated by console command, especially complicated ones, you can totally do

rails g migration my_awesome_100_line_change_to_model_xyz

and go ahead edit in the empty generated method, just need to be aware of both up and down phase by yourself this case

miushock
  • 1,087
  • 7
  • 19
2

You are doing it Wrong.

The syntax should be

rails g migration add_column_first_name_to_users first_name:string

rails g migration add_column_last_name_to_users last_name:string

or simply

rails g migration add_first_name_to_users first_name:string
rails g migration add_last_name_to_users last_name:string

or

The best way is to generate them in single Command(@RSB said).

Pavan
  • 33,316
  • 7
  • 50
  • 76
  • picking your answer as it was the first one. =) But I have to admit that I'm a tad puzzled. My original migration was based at this stackoverflow answer: http://stackoverflow.com/questions/4834809/adding-a-column-to-a-table-in-rails. It doesn't mention the "add_column" part. What did I misunderstand in that answer? – user273072545345 Mar 21 '14 at 05:52
  • look at the accepted answer.The command can also written as add_first_name_to_users.Both are equally same. – Pavan Mar 21 '14 at 05:54
  • That answer merely saying you need to add new migration if old ones has already been rake migrated. Your rails migration file tells how to change the database model and the rake command actually changes it. You cant run same Migration twice unless you clobber your db and start from scratch – miushock Mar 21 '14 at 05:55
  • oh! I missed the "add" part! Groan! – user273072545345 Mar 21 '14 at 05:57
0

Sorry for being late to the party, but I am not sure why nobody mentioned these other ways:

rails g migration add_column_first_name_and_last_name_to_user first_name:string last_name:string

or:

rails g migration add_column_first_name_and_last_name_to_users first_name:string last_name:string

or:

rails g migration add_first_name_and_last_name_to_user first_name:string last_name:string

or:

rails g migration add_first_name_and_last_name_to_users first_name:string last_name:string

Will generate:

class AddFirstNameAndLastNameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :first_name, :string
    add_column :users, :last_name, :string
  end
end

You missed to append add_ in your command to first parameter. Of course, class name AddFirstNameAndLastNameToUsers will change according to the first parameter you pass to rails g migration command.

Surya
  • 15,703
  • 3
  • 51
  • 74
  • no worries about being late to the party. More information is always welcome. =) Thanks for taking the time to add additional information. – user273072545345 Sep 26 '14 at 01:46