Bringin back from the dead and why I love working on the Web

December 23, 2017

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. ... Read more

Retrieving random row from a table

April 5, 2011

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. ... Read more

Highlighting current link within a navigation list in rails

March 28, 2011

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. "this.addClassName('youarehere');this.siblings().each(function(s){ s.removeClassName('youarehere');});", :class = '', :id = 'home' % "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. ... Read more

Centering a Fixed-Sized Element

March 22, 2011

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 */ }

Re-initializing table sequence in postgresql

March 15, 2011

Re-initializing a table sequence is quite normal and required in some cases. I had to do that recently. If the table has data and needs to be preserved, then temporarily you have to move the data, delete the data in original table and re-initialize the sequence and copy the original data again. Use copy column names to reinitialize key sequence. In current versions of PostgreSQL you can specify column names with the COPY command to copy in or out only specific columns. ... Read more

Loading data from multiple files in Ruby using Hash

February 21, 2011

Lately, I have been working applications which require data from an external source to be loaded into the local database. One such application is Sydrea (in the making!), which requires the Drug information to be loaded. Drugs@FDA is freely downloadable compressed zip file which contains about 9 CSV files having Drug related data. The usual path to data loading is through a rails runner script. I was tired of writing, testing different scripts to load data. ... Read more

Adding close link to flash messages

February 19, 2011

The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create action that sets flash[:notice] = "Successfully created" before redirecting to a display action that can then expose the flash to its template. Actually, that exposure is automatically done. ... Read more

Connecting to two or more databases from a Rails app

January 18, 2011

The current project I am working on is a re-write of an old application, which essentially was build as an prototype. At that time it was put off due to reasons unknown and now the client has come back for the same application with a new set of requirements and changes to the prototype. Its a spatial application where everything is centered around 'location' and location information (pincodes etc. ... Read more

© 2017 | Follow on Twitter