Find Random plugin

Plugin details

The plugin provides a (somewhat) scalable way to select random rows from a table. It runs significantly faster than asking most databases to "ORDER BY RAND()". Behind the scenes it grabs all ids that match the query conditions, then selects a random subset of these for which it then asks the database for the full records. The initial query is done outside of ActiveRecord to avoid needless object instantiation.

If only one result is requested a different strategy is used. First a query is done to determine the number of results, then a single result is selected using a random offset. This removes the memory inefficiency of the method used when selecting multiple results.

Repositoryhttp://pub.svn.greenriver.org/find_random Author Ben Tucker Tags random, find LicenseUnknown

Documentation

Install the plugin:
ruby script/plugin install http://pub.svn.greenriver.org/find_random

Example

  # select 5 random items that match the :conditions  
  @items = Item.random(5, :conditions => ["col1 = ? or col2 = ?", val1, val2])  

  # select any 5 random items  
  @items = Item.random(5)  

Further Documentation

There is currently no advanced documentation for this plugin.

New documentation

Edit plugin | (0 older versions) | Last edited by: Guest, over 2 years ago