I have Ruby on Rails 2.3.x. I have a database for a learning management system.
Users are given a user ID, and take quizzes. If a user takes any number of quizzes, I want the average score to display. The score is listed in the quiz_results
table as a float between 0 and 1, hence the * 100
and .round
, with a user_id
row as well. But if a user hasn't taken any quizzes, I want "No quizzes taken"
to appear.
Here's the code:
-if QuizResult.find_by_user_id(@user_id).present?
="#{(QuizResult.average('score', :conditions => 'user_id = #{@user.id}') * 100).round}%"
-else
%em No quizzes taken
The problem is that no matter what, even if there is a quiz_results
entry, the "No quizzes taken"
still appears.
What am I doing wrong?
You're testing whether:
exists, but I suspect you meant: