Almost all Ruby on Rails developers might come across scenario where they need to add a new column with a default value to one of the database tables. Most of us (including me) would write following migration statement -

add_column :table_name, :column_name, :boolean, default: false

This is a good practice but would cause downtime if the table has large number of records. It took 3 secs when I ran the migration for a table having 50k records.

-- add_column(:table_name, :column_name,...

Read complete blog post

Run Helper methods in the console

$rails c
= helper.number_to_currency(100)
= "$100.00"
= helper.time_ago_in_words(3.days.ago)
= "3 days"

Shorthand Migrations

$ rails g resource person first_name:string last_name:string email:string token:string

You can write above command as-

$ rails g resource person first_name last_name email token

Both will generate same migration:

class CreatePeople < ActiveRecord::Migration
 def change
 create_table :people do |t|
 t.string :first_name
 t.string :last_name
 t.string :email
 t.string :token

 t.timestamps
 end
 end
end

Add Indexes to migrations

$rails g resource person...
< ActiveRecord::Migration def change create_table :people do |t| t.string :first_name t.string :last_name t.string :email t.string :token t.timestamps end end end

Read complete blog post

Recently we had requirement in Mamasan project to migrate Heroku PostGreSQL database to SoftLayer (dedicated server) psql database

One major issue we faced during migration (import of DB to SoftLayer DB) using "pg_restore" is the version conflict between the dump file that Heroku creates (for pgsql backups) and the old version 8.4. of PostGreSQL that SoftLayer instance has.

We upgraded PostGreSQL to version 9.1 on SoftLayer (remember to upgrade version above Heroku dump file or use same...

Read complete blog post