database migration - Change foreign key column name in rails -
i have migration class project this
class createprojects < activerecord::migration def change create_table :projects |t| t.string :title t.text :description t.boolean :public t.references :user, index: true, foreign_key: true t.timestamps null: false end end end
it creates column name user_id in projects table want name column owner_id can use project.owner instead of project.user.
you can 2 ways:
#app/models/project.rb class project < activerecord::base belongs_to :owner, class_name: "user", foreign_key: :user_id end
--
$ rails g migration change foreignkeyforprojects # db/migrate/change_foreign_key_for_projects.rb class changeforeignkeyforprojects < activerecord::migration def change rename_column :projects, :user_id, :owner_id end end $ rake db:migrate
Comments
Post a Comment