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
| @@ -8,4 +8,3 @@ justinfrench-formtastic --version '0.2.2' --source gems.github.com | @@ -8,4 +8,3 @@ justinfrench-formtastic --version '0.2.2' --source gems.github.com | ||
| 8 | thoughtbot-clearance --version '0.8.2' --source gems.github.com | 8 | thoughtbot-clearance --version '0.8.2' --source gems.github.com |
| 9 | josevalim-inherited_resources --version '0.9.1' --source gems.github.com | 9 | josevalim-inherited_resources --version '0.9.1' --source gems.github.com |
| 10 | activemerchant --version 1.4.2' | 10 | activemerchant --version 1.4.2' |
| 11 | - |
app/models/question.rb
| @@ -22,20 +22,14 @@ class Question < ActiveRecord::Base | @@ -22,20 +22,14 @@ class Question < ActiveRecord::Base | ||
| 22 | end | 22 | end |
| 23 | 23 | ||
| 24 | def picked_prompt | 24 | def picked_prompt |
| 25 | - pc = self.prompts_count < 1 ? 2 : self.prompts_count | ||
| 26 | begin | 25 | begin |
| 27 | - @p = prompts.find(rand(pc-1) + prompts.first.id) | 26 | + @p = prompts.first(:order => 'RANDOM()') |
| 28 | end until @p.active? | 27 | end until @p.active? |
| 29 | return @p | 28 | return @p |
| 30 | end | 29 | end |
| 31 | - | ||
| 32 | 30 | ||
| 33 | def picked_prompt_id | 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 | end | 33 | end |
| 40 | 34 | ||
| 41 | def left_choice_text(prompt = nil) | 35 | def left_choice_text(prompt = nil) |