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

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