我正在努力弄清楚如何实现这个计数。 模型是用户、测试、等级
用户 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
最佳答案
你应该能够做到这一点:
class User < ActiveRecord::Base
has_many :tests
has_many :grades, through: :tests
end
user = User.first
user.grades.passed.count
user.grades.failed.count
关于ruby-on-rails - 用户 has_many 测试,测试 has_many 成绩。如何计算成绩?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13022941/