heroku: multiple accounts and multiple environments

use heroku accounts plugin to setup multiple accounts.

– set your work account for production

heroku accounts:set work

– create a production environment
heroku create --remote production

– change production git url
git remote set-url production https://git.heroku.com/my-app.git

– deploy your app to production
git push production master

– Advanced: Linking local branches to remote apps
git push heroku staging:master

– run migration if any
heroku run rake db:migrate --remote production


Rails: add foreign key to existing column with different column names

Reference migration

class CreateBranches < ActiveRecord::Migration[5.1]
  def change
    create_table :branches do |t|
      t.string :name
      t.string :email
      t.string :phone
      t.string :alternate_phone
      t.string :mobile
      t.integer :contact_one_id, index: true #add columns as integer
      t.integer :contact_two_id, index: true #add columns as integer
      t.references :address, foreign_key: true

    #add foreign key constraints on columns
    add_foreign_key :branches, :employees, column: :contact_one_id
    add_foreign_key :branches, :employees, column: :contact_two_id

and put belongs_to relation in Branch model

class Branch < ApplicationRecord
  belongs_to :contact_one, class_name: 'Employee'
  belongs_to :contact_two, class_name: 'Employee'