Commit 5883f6f9cc7c9a985ba489c067b560f3898b6b9a
1 parent
eaf03aa8
Exists in
master
and in
1 other branch
fast, correct random prompt picking
Showing
2 changed files
with
2 additions
and
9 deletions
Show diff stats
app/models/question.rb
... | ... | @@ -22,20 +22,14 @@ class Question < ActiveRecord::Base |
22 | 22 | end |
23 | 23 | |
24 | 24 | def picked_prompt |
25 | - pc = self.prompts_count < 1 ? 2 : self.prompts_count | |
26 | 25 | begin |
27 | - @p = prompts.find(rand(pc-1) + prompts.first.id) | |
26 | + @p = prompts.first(:order => 'RANDOM()') | |
28 | 27 | end until @p.active? |
29 | 28 | return @p |
30 | 29 | end |
31 | - | |
32 | 30 | |
33 | 31 | def picked_prompt_id |
34 | - pc = self.prompts_count < 1 ? 2 : self.prompts_count | |
35 | - begin | |
36 | - @i = rand(pc-1) + prompts.first.id #try caching this first prompt value at some point | |
37 | - end until prompts.find(@i).active? | |
38 | - return @i | |
32 | + picked_prompt.id | |
39 | 33 | end |
40 | 34 | |
41 | 35 | def left_choice_text(prompt = nil) | ... | ... |