If you have already run the migration then, you could just add a new migration as
rails generate migration AddClientToProjects client:references
This will generate a migration as below:
class AddClientToProjects < ActiveRecord::Migration
def change
add_reference :projects, :client, index: true
end
end
and do rake db:migrate
.
If you want to add the reference in the CreateProjects
migration itself.
Then do the following:
Rollback the migration,if its already run
rake db:rollback VERSION=version_number
where,
Replace version_number with the version number mentioned in the migration filename.
For eg: If your migration filename is 20140125190622_create_projects.rb
then command should be
rake db:rollback VERSION=20140125190622
Destroy the current migration using
rails destroy migration CreateProjects
and create it again using:
rails generate migration CreateProjects name start_date:datetime end_date:datetime active:boolean client:references
This will create a migration as follows:
class CreateProjects < ActiveRecord::Migration
def change
create_table :projects do |t|
t.string :name
t.datetime :start_date
t.datetime :end_date
t.boolean :active
t.references :client, index:true # You could also add it manually to existing migration
t.timestamps
end
end
end
Run rake db:migrate
after this.