Rails/AR: Fast random records for ActiveRecord
Database (e.g. MySql) :order=>'rand()' is slow.
script/plugin install git://github.com/grosser/random_records.git
sudo gem install random_record
###Find many in single random cluster (no duplicates) Model.random(1) == [Model(id:322)] Model.random(3) == [Model(id:113),Model(id:112),Model(id:114)] Model.with_valid_email.random(3) == [Model(id:114),Model(id:112),Model(id:113)]
###Find many in many random clusters
The smaller the slower (each cluster = 1 request)
May include duplicates so use .uniq
on results.
Model.random(3, :cluster_size=>1) == [Model(id:112),Model(id:98),Model(id:214)]
###Find one random Model.random == Model(id:234) Model.with_valid_email.random == Model(id:123)
Michael Grosser [email protected] Hereby placed under public domain, do what you want, just do not hold me accountable...