Class: Mongoid::Criteria

Inherits:
Object
  • Object
show all
Defined in:
lib/mongoid/sample.rb

Instance Method Summary collapse

Instance Method Details

#sample(n = 1) ⇒ Object



6
7
8
9
10
11
12
13
14
# File 'lib/mongoid/sample.rb', line 6

def sample(n = 1)
  indexes = (0..count - 1).sort_by { rand }.slice(0, n)

  if n == 1
    skip(indexes.first).first
  else
    indexes.map { |i| skip(i).first }
  end
end