Reputation: 1566
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?
Upvotes: 1
Views: 4248
Reputation: 16002
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.
Upvotes: 0
Reputation: 33542
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).
Upvotes: 2
Reputation: 1097
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
Upvotes: 3
Reputation: 17834
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
Upvotes: 4