3

我正在努力弄清楚如何实现这个计数。模型是用户、测试、等级

用户 has_many 测试,测试 has_many 成绩。

每个等级都有一个计算分数(strong_pass、pass、fail、strong_fail)。

我怎样才能得到每个年级类别的计数?

为清楚起见,用户可能会参加 4 次数学测试,直到他们通过为止。他们可能会获得分数(通过、失败等)

但我想知道,在为用户 X 进行的所有测试中,有多少次通过,多少​​次失败?

user.tests.grades.passed.count 是我希望的工作。但没有
(我确实在等级模型中为“通过”、“失败”等命名了范围)

Class Grade

  def self.passed
    where(:grade => "passed")
  end

  def self.failed
    where(:grade => "failed")
  end

end
4

1 回答 1

4

你应该能够做到这一点:

class User < ActiveRecord::Base
  has_many :tests
  has_many :grades, through: :tests
end

user = User.first

user.grades.passed.count
user.grades.failed.count
于 2012-10-23T03:35:08.053 回答