MonoForge

porras / random_items

random_items

Public

Rails plugin to pick random items from a model (without using :order => "RAND()")

10 filesupdated Jun 18, 2026

README

random_items is a Rails plugin which allows to pick random items from an ActiveRecord model (without using <tt>:order => "RAND()"</tt> which is not a good idea in terms of performance).

The interface is almost the same as that of <tt>ActiveRecord::Base.find</tt>.

Some examples:

Picking a random item:

Model.random(:first)

Picking 5 random items:

Model.random(:all, :limit => 5)

Pickings a random item using standard <tt>ActiveRecord::Base.find</tt> options, such as <tt>:include</tt>

Model.random(:first, :include => :relation)

In that last case, you are expected not to use options which don't make sense, such as <tt>:order</tt> =;-)

Copyright (c) 2008 Sergio Gil, released under the MIT license