@@ -6,10 +6,9 @@ class FlashcardsNeedingReviewQuery
   end
 
   def run
-    Flashcard.
-      all.
-      map { |q| q.most_recent_attempt_for(@user) }.
-      select { |a| a.confidence > 0 && a.confidence < CONFIDENCE_THRESHOLD }.
+    LatestAttempt.by(@user).
+      confidence_below(CONFIDENCE_THRESHOLD).
+      includes(:flashcard).
       map(&:flashcard)
   end
 end
class FlashcardsNeedingReviewQuery
  CONFIDENCE_THRESHOLD = 4

  def initialize(user)
    @user = user
  end

  def run
    Flashcard.
      all.
      map { |q| q.most_recent_attempt_for(@user) }.
      select { |a| a.confidence > 0 && a.confidence < CONFIDENCE_THRESHOLD }.
      map(&:flashcard)
  end
end

I could re-work the database view so it was a view of flashcard data (rather than attempts) to avoid this `map` call here, but this seemed more straightforward and flexible. Thoughts?