Commit 5883f6f9cc7c9a985ba489c067b560f3898b6b9a

Authored by Pius Uzamere
1 parent eaf03aa8

fast, correct random prompt picking

Showing 2 changed files with 2 additions and 9 deletions   Show diff stats
.gems
... ... @@ -8,4 +8,3 @@ justinfrench-formtastic --version '0.2.2' --source gems.github.com
8 8 thoughtbot-clearance --version '0.8.2' --source gems.github.com
9 9 josevalim-inherited_resources --version '0.9.1' --source gems.github.com
10 10 activemerchant --version 1.4.2'
11   -
... ...
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)
... ...