From 74088ac949f675a6eb268e6180a3b8d278a429e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Thu, 28 Mar 2019 15:03:06 +0100 Subject: [PATCH] Make random IDs with the same seed consistent The order of the array before being shuffled needs to be the same if we want to have the same array after being shuffled with a certain seed. We were using `pluck(:id)`, which doesn't guarantee the order of the elements returned. Replacing it with `order(:id).pluck(:id)` adds an `ORDER BY` clause and so guarantees the order of the elements. --- app/models/concerns/randomizable.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/models/concerns/randomizable.rb b/app/models/concerns/randomizable.rb index c346f1f02..98a86b426 100644 --- a/app/models/concerns/randomizable.rb +++ b/app/models/concerns/randomizable.rb @@ -3,7 +3,7 @@ module Randomizable class_methods do def sort_by_random(seed = rand(10_000_000)) - ids = pluck(:id).shuffle(random: Random.new(seed)) + ids = order(:id).pluck(:id).shuffle(random: Random.new(seed)) return all if ids.empty?