6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
# File 'lib/quick_random_records.rb', line 6
def self.random_records(quantity)
id_range = 1..self.last.id
sample_ids = [*id_range].sample(quantity)
samples = self.where(id: sample_ids)
while samples.size < quantity
complement = []
while complement.empty?
complement_id = rand(id_range)
next if sample_ids.include?(complement_id)
sample_ids << complement_id
complement = self.where(id: complement_id)
end
samples = samples.or(complement)
end
samples
end
|