Permalink
Browse files

Working around a bug in rocket_tag.

rocket_tag's `tagged_with` method fails when running on top of PostgreSQL.
See bradphelan/rocket_tag#6
  • Loading branch information...
1 parent 7b63cd8 commit 178d807848e453ef114665de000cbb22dc137d2c @dslh committed Mar 16, 2012
Showing with 17 additions and 4 deletions.
  1. +17 −4 app/controllers/quizzes_controller.rb
@@ -1,11 +1,24 @@
class QuizzesController < ApplicationController
- # GET /quizzes/tag+tag2/20
+ # GET /quizzes/language/1/tag+tag2/20
def show
@tags = params[:tags].split('+')
@language = Language.find(params[:language])
- @phrases = @language.phrases.tagged_with(@tags)
- length = [params[:length].to_i || 10, @phrases.length].min
- @phrases = @phrases.shuffle[0...length]
+# The below code is preferred but at present
+# there is an issue with rocket_tag + postgresql.
+# @phrases = @language.phrases.tagged_with(@tags)
+# length = [params[:length].to_i || 10, @phrases.length].min
+# @phrases = @phrases.shuffle[0...length]
+ @phrases = Phrase.find_by_sql("
+ SELECT * FROM phrases WHERE #{@tags.length} = (
+ SELECT count(*) FROM taggings JOIN tags
+ ON tags.id = taggings.tag_id
+ WHERE tags.name IN (
+ #{@tags.collect { |t| "'#{t}'" }.join(',')}
+ ) AND taggings.taggable_id = phrases.id
+ AND taggings.taggable_type = 'Phrase'
+ ) AND language_id=#{@language.id}
+ ORDER BY random() LIMIT #{params[:length] || 10}
+ ")
respond_to do |format|
format.html { render 'quiz' }

0 comments on commit 178d807

Please sign in to comment.