如何根据 Rails3 中模型的子模型计算模型中的值?
我是 Rails 新手,我可以想出几种方法来解决这个问题,但我想用“rails”方式来解决。我的问题是我有一个模型 - 考试 - 其中:有很多问题。我想在考试模型中编写一个函数来计算考试分数(并将其存储在考试模型中)。为此,我需要从属于考试的问题对象中读取数据。
任何提示或文档链接将不胜感激。
I'm new to rails and I can think of several ways to solve this problem, but I'd like to do it the "rails" way. My problem is that I have a model - exam - which :has_many questions. I'd like to write a function in the exam model which will calculate the score for the exam (and store it in the exam model). To do this, I need to read data from the question objects that belong to the exam.
Any tips or links to documentation would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只要您没有写任何有关数据结构的内容,我们就可以想象您的每个问题都有布尔
正确
字段。并且您的考试有 Integertotal_score
字段。因此,在考试后,应该计算所有正确答案的问题:As far as you didn't write anything about your data structure let's imagine that your every question has_got Boolean
correct
field. And your exam has got Integertotal_score
field. So after examing it should count all questions with correct answer: