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

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