Sha256: e66d9b3df65521f9b190ad27230bfc2ed49d5ede6cd915356991107ac1fe61f9
Contents?: true
Size: 547 Bytes
Versions: 1
Compression:
Stored size: 547 Bytes
Contents
require "active_sample/version" require "active_record" module ActiveSample class NegativeSampleError < ArgumentError end def sample(n = 1) if n < 0 raise NegativeSampleError.new("negative sample number".freeze) end max_id = maximum(primary_key) return nil if max_id == nil if n == 1 while !(found = find_by(primary_key => 1 + rand(max_id))); end found elsif n == 0 none else where(primary_key => ids.sample(n)) end end end ActiveRecord::Base.send :extend, ActiveSample
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
active_sample-1.0.2 | lib/active_sample.rb |