If you had visited this site last week, you would’ve found the site basically unchanged since mid-2011, a blog frozen in time as if awaiting a next post that never comes.
Not any more. I took time over the weekend to bring back alive the blog which was dead, with new goals. Self hosting a personal website is just hard, still. But, I am now commited to write and share more of what I learn, as I learn. You'll see a complete overhaul of this website soon.

Why I love working on the web

Working on the web is hard and challenging. Competing priorities from clients, managing the expectations of people who don't understand technology, and the ever expanding and evolving web-stack all contribute to it's challenges. Most days I'll leave with more to do that I got done, and yet I love it. This crazy environment attracts really smart people. I have one of the most talented teams I know of and they're all really brilliant at what they do. I feel it has been a privilege to work everyday with such bright people.


I started as a Java developer then jumped on the Rails bandwagon in mid-2008 and loved it. The first Rails project I worked on, vaccidate.in: a free service to get reminded of your kids' Vaccination schedule, is still on. Of late, I've been working with and exploring JavaScript technologies both in the front-end with AngualarJS and back-end with NodeJS. The JavaScript scene is currently in hyper mode and there goes by not a single week without a new framework or library release. Its brilliant.


I have been coding less and less for atleast and year now especially after I started working and managing this team which I intend to change. This year, I want to dive deep into JavaScript and involve myself with all things web, exploring web standards, architectures of large scale applications and contributing to open-source.

To retrive a random row from a table you have do just this:

 Model.find :first, :offset => ( Model.count * rand ).to_i 

This is fast and requires no custom SQL queries! All this does is count number of rows in a table and select one row at some offset while still having the table ordered by primary key.

It uses offset (limit), not ID number, so it chooses n-th found row, not a row with ID equal to n. So if you have IDs like 1, 2, 4, 8and get 3 at random, this method will take 3rd row, which has ID = 4.

You can recover MySQL database server password with following five easy steps.

Step # 1: Stop the MySQL server process.

 # sudo /etc/init.d/mysql stop 

Step # 2: Start the MySQL (mysqld) server/daemon process with the --skip-grant-tables option so that it will not prompt for password.

 # sudo mysqld_safe --skip-grant-tables & 

Step # 3: Connect to mysql server as the root user.

 # mysql -u root 

Step # 4: Setup new mysql root account password i.e. reset mysql password.

mysql> use mysql;
mysql> update user set password=PASSWORD("NEW-ROOT-PASSWORD") where User='root';
mysql> flush privileges;
mysql> quit

Step # 5: Exit and restart the MySQL server.

# sudo /etc/init.d/mysql stop

# sudo /etc/init.d/mysql start

# mysql -u root -p

You have just changed the password for the root user of mysql database.

It's very common to want to highlight the current link within a navigation list for the current action being performed. If you are using Ajax, then this is easy, handle the onclick event to highlight the current link.

<%= link_to 'Home', 'javascript:void(0);', 
  :onclick => "this.addClassName('youarehere');this.siblings().each(function(s){
  s.removeClassName('youarehere');});", :class => '', :id => 'home' %> 

<%= link_to 'News', 'javascript:void(0);', 
  :onclick => "this.addClassName('youarehere');this.siblings().each(function(s){
  s.removeClassName('youarehere');});", :class => '', :id => 'news' %> 

Make sure you have a CSS class called youarehere. The following CSS is just an example.

.youarehere {
    font-size: 11pt;
    text-decoration: underline;

If you are not using Ajax, you need to know what the current action and controller is. But how to know what the currect action is (without doing some ugly URL parsing)?

The first thing to do is add the following code to your application_helper.rb file

def section_link(name, options)
  action      = options[:action] || 'index'
  controller  = options[:controller] 
    if action == @current_action && controller == @current_controller
      link_to(name, options, :class => 'youarehere')
        link_to(name, options)

The above method always expects an :controller key to be passed in with the options hash. If you don't pass the optional :action key, it will assume 'index' as the action being performed.

We need to know what the current_controller and current_action is for the above method to work. Finally put this in your application_controller.rb file, as it makes the current executing action and controller name available to the helper.

before_filter :instantiate_controller_and_action_names

def instantiate_controller_and_action_names
    @current_action         = action_name
    @current_controller = controller_name

In your view you have to use:

<%= section_link( "Home", :controller => 'welcome', :action => 'index' ) %> |

<%= section_link( "News", :controller => 'news', :ref => 'home' ) %>

Here is one way to center a fixed-width/fixed-height div at the center of its container. This could be adapted to centering text, images, etc. within their containers. Essentially, we do a bit of arithmetic to get the fixed-sized element centered using absolute positioning and margins. Note that the parent container must have a position: relative property for this to work.

div { position: absolute; top: 50%; left: 50%; width: 400px; height: 300px; margin-top: -150px; /* 1/2 of your element height*/ margin-left: -200px; /* 1/2 of your element width */ }