Rails plugin to pick random items from a model (without using :order => "RAND()")
Ruby
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
tasks
test
.gitignore
MIT-LICENSE
README
Rakefile
init.rb
install.rb
uninstall.rb

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