[英]Ruby on Rails Condition Broken
I have Ruby on Rails 2.3.x. 我有Ruby on Rails2.3.x。 I have a database for a learning management system.
我有一个用于学习管理系统的数据库。
Users are given a user ID, and take quizzes. 将为用户提供用户ID,并进行测验。 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. 分数在
quiz_results
表中以0到1之间的浮点数(即* 100
和.round
,并且还有一个user_id
行。 But if a user hasn't taken any quizzes, I want "No quizzes taken"
to appear. 但是,如果用户未参加任何测验,我希望显示
"No quizzes taken"
。
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. 问题是,不管就算有什么,一个
quiz_results
条目中, "No quizzes taken"
仍然出现。
What am I doing wrong? 我究竟做错了什么?
You're testing whether: 您正在测试是否:
QuizResult.find_by_user_id(@user_id)
exists, but I suspect you meant: 存在,但我怀疑您的意思是:
QuizResult.find_by_user_id(@user.id)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.