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

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -