本文介绍了Ruby on Rails条件损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Ruby on Rails2.3.x.我有一个用于学习管理系统的数据库.

I have Ruby on Rails 2.3.x. I have a database for a learning management system.

为用户提供了一个用户ID,并进行测验.如果用户参加任何测验,我希望显示平均分数.分数在quiz_results表中以0到1之间的浮点数形式列出,因此在* 100.round之间也有一个user_id行.但是,如果用户没有进行任何测验,我希望显示"No quizzes taken".

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.

代码如下:

-if QuizResult.find_by_user_id(@user_id).present?
    ="#{(QuizResult.average('score', :conditions => 'user_id = #{@user.id}') * 100).round}%"
-else
    %em No quizzes taken

问题是,无论如何,即使有quiz_results条目,也会出现"No quizzes taken".

The problem is that no matter what, even if there is a quiz_results entry, the "No quizzes taken" still appears.

我在做什么错了?

推荐答案

您正在测试是否:

QuizResult.find_by_user_id(@user_id)

存在,但我怀疑您的意思是

exists, but I suspect you meant:

QuizResult.find_by_user_id(@user.id)

这篇关于Ruby on Rails条件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 11:32