git: check file commit history

git log -p -- filename

Advertisements

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

      t.timestamps
    end
    #add foreign key constraints on columns
    add_foreign_key :branches, :employees, column: :contact_one_id
    add_foreign_key :branches, :employees, column: :contact_two_id
  end
end

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'
end

Rails 4: hack for creating mysql table without auto_increment

I came across an issue when I was trying to create a zipcodes table with `code` as primary key and I didn’t want AUTO_INCREMENT.

I wrote below migration-


class CreateZipcodesTable < ActiveRecord::Migration def change create_table :zipcodes, :id => false do |t|
t.integer :code, :null => false
t.integer :state_id, null: false
t.string :city, limit: 255
t.string :state_code, limit: 2, null: false
end
add_index :zipcodes, :code, :unique => true
end
end