Tuesday, August 28, 2012

Add index on PostgreSQL table column for in-built full text search in Rails

Applicable for PG 8.4 and above.

class AddIndexesToListings < ActiveRecord::Migration
  def change
    execute <<-SQL
      create index on listings using gin(to_tsvector('english', title));
      create index on listings using gin(to_tsvector('english', description));

Upgrade Heroku db to new PostgreSQL Dev addon

heroku addons:add heroku-postgresql:dev
heroku pg:promote NEW_DB_COLORFUL_NAME

Restarting crashed workers in Heroku

Presumably we can issue the restart command

heroku restart worker.1
But don't bother. It didn't work for me. Just kill it and spin up another worker.

heroku ps:scale worker=0
heroku ps:scale worker=1

Configure PostgreSQL full-text search using pg_search gem for partial word match

class Listing < ActiveRecord::Base
  include PgSearch
  pg_search_scope :search_by_title_and_description, against: [:title, :description], using: { tsearch: { prefix: true } }

Monday, August 27, 2012

Run delayed jobs on Heroku with queue names

heroku config:set QUEUE=xxx
heroku ps:scale worker=1

Friday, August 24, 2012

Transfer domain from a Heroku Zerigo account to another

Say you have a domain foo.com that's tied to a Heroku Zerigo addon, you can import them and all its CNAME entries into another account very easily.


1. In old Zerigo account, click on domain  >> "Tools" >> "Reassign domain"
2. Click "Generate a reassignment token" button and copy the token string
3. In new Zerigo account, click on "+ add / import" >> "Add reassigned domain"
4. Enter the transferring domain name and the copied reassignment token
5. Click "Add" and voila!

Thursday, August 16, 2012

Share methods between controller and views in Rails

The key is to put methods into ApplicationHelper and call them in the controller

module ApplicationHelper
  def user_unauthorized?

class HomeController < ApplicationController
  def index
    redirect_to '/foo' if view_context.user_unauthorized?

Tuesday, August 14, 2012

Passing array of elements as multiple arguments in a ruby method

Suppose we wanna pass an array of elements like this

keys = %w{

Then we need to prepend the variable with * as arguments


Wednesday, August 8, 2012

Multiple heroku apps on different subdomains managed by Zerigo

Suppose you have 3 different heroku apps but you want them to have their own domains as well as subdomains.

a.herokuapps.com -> a.foo.com or a.com
b.herokuapps.com -> b.foo.com or b.com
c.herokuapps.com -> c.foo.com or c.com
If you wanna use the Zerigo DNS addone, the trick is to use addon in ONLY ONE of the heroku apps. In this instance, I'll add the Zerigo addon into a.herokuapps.com. From the addon dropdown, you should be able to click on the configure button and that'll bring you to the Zerigo admin page. Suppose you've already point the foo.com, a.com, b.com and c.com nameservers to Zerigo nameservers, you should add the subdomains as CNAME entries.

a.foo.com CNAME as a.herokuapps.com
b.foo.com CNAME as b.herokuapps.com
c.foo.com CNAME as c.herokuapps.com
And add all the domains and subdomains as custom domains in each heroku app and should be good to go once the DNS gets propagated.