Ruby on Rails Condition Broken

2019-08-18 17:38发布

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?

1条回答
▲ chillily
2楼-- · 2019-08-18 18:10

You're testing whether:

QuizResult.find_by_user_id(@user_id)

exists, but I suspect you meant:

QuizResult.find_by_user_id(@user.id)
查看更多
登录 后发表回答