Module: ActiveSample
- Defined in:
- lib/active_sample.rb,
lib/active_sample/version.rb
Defined Under Namespace
Classes: NegativeSampleError
Constant Summary collapse
- VERSION =
"1.0.1"
Instance Method Summary collapse
Instance Method Details
#sample(n = 1) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/active_sample.rb', line 8 def sample(n = 1) if n < 0 raise NegativeSampleError.new("negative sample number".freeze) end max_id = maximum(:id) return nil if max_id == nil if n == 1 while !(found = find_by(id: 1 + rand(max_id))); end found elsif n == 0 none else where(id: ids.sample(n)) end end |