我在exams_helper.rb
中构建了两个方法用于视图:
<% @topic_questions.each do |topic_question| %>
<tr>
<td><%= topic_question.topic.name %></td>
<td><%= correct_questions(@exam_result.exam_id, topic_question.topic_id) %></td>
<td><%= number_to_percentage(ratio(@exam_result.exam_id, topic_question.topic_id), precision: 0) %></td>
</tr>
<% end %>
计算主题正确问题数量的方法:
def correct_questions(exam_id, topic_id)
total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count
correct.to_s + '/' + total.to_s
end
计算正确率百分比的方法
def ratio(exam_id, topic_id)
total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count
ratio = (correct.to_f/total).round(2)*100
if ratio.nan?
ratio = 0
else
ratio
end
end
重复这些代码:
total = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id).count
correct = ExamQuestion.where(exam_id: exam_id, topic_id: topic_id, correct: true).count
如何更好地编写这些方法?
答案 0 :(得分:4)
在我看来,这些方法应该依赖于您的模型,因为它们的目的是从数据库计算数据。此外,通过在Model层中编写这些方法,可以避免在控制器,视图或视图助手中出现重复。
查看帮助程序只应用于“视图逻辑”方法,这些方法在视图上下文之外没有多大意义。
correct_questions 和比率似乎与ExamResult对象密切相关,我们可以想象以下实现:
class ExamResult
has_many :exam_questions
def correct_questions_ratio(topic)
ratio = (correct_questions(topic).to_f/total_questions(topic)).round(2)*100
if ratio.nan?
ratio = 0
else
ratio
end
end
def total_questions(topic)
#To avoid recomputing the result from db we memoize it for each topic.
@total_questions ||= {}
@total_questions[topic] ||= exam_questions.where(:topic_id => topic.id).count
end
def correct_questions(topic)
#To avoid recomputing the result from db we memoize it for each topic.
@correct_questions ||= {}
@correct_questions[topic] ||= exam_questions.where(:topic_id => topic.id, :correct => true).count
end
end
记忆是一种“缓存”形式,以避免多次重新计算相同的结果。你可以找到很多关于它的文章。这是一个很好的:http://www.railway.at/articles/2008/09/20/a-guide-to-memoization/
最后,您将在视图中包含以下代码。帮助程序不再是必需的,但您仍然可以编写辅助方法来构造“正确/完整”部分,将ExamResult实例 - @exam_result - 作为参数。
<% @topic_questions.each do |topic_question| %>
<tr>
<td><%= topic_question.topic.name %></td>
<td><%= @exam_result.correct_questions(topic_question.topic) %>/<%= @exam_result.total_questions(topic_question.topic)%></td>
<td><%= number_to_percentage(@exam_result.correct_questions_ratio(topic_question.topic)), precision: 0) %></td>
</tr>
<% end %>
答案 1 :(得分:1)
在你的模特中:
scope :exam, lambda { |exam_id| where(exam_id: exam_id) }
scope :topic, lambda { |topic_id| where(topic_id: topic_id) }
scope :correct, lambda { where(correct: true) }
在你的帮手中:
def get_total_and_correct_count_for(exam_id, topic_id)
[
ExamQuestion.exam(exam_id).topic(topic_id).count,
ExamQuestion.exam(exam_id).topic(topic_id).correct.count
]
end
def correct_questions(exam_id, topic_id)
total, correct = get_total_and_correct_count_for(exam_id, topic_id)
correct.to_s + '/' + total.to_s
end
def ratio(exam_id, topic_id)
total, correct = get_total_and_correct_count_for(exam_id, topic_id)
ratio = (correct.to_f/total).round(2)*100
if ratio.nan?
ratio = 0
else
ratio
end
end
旁注:
在帮助程序中执行此类数据库交互感觉很奇怪。
我首先考虑了memoizing,但ActiveRecord提供了内置缓存
如果它在一个循环中,请考虑缓存结果以使它们在请求中保持不变,因为它会损坏db
答案 2 :(得分:0)
首先,为什么不在ExamQuestion
模型上创建几个方法,而不是每次都使用where
?
class ExamQuestion
#...
def self.total_count(exam_id, topic_id)
where(exam_id: exam_id, topic_id: topic_id).count
end
# create similar method for "correct" count
end
然后,我会完全从视图中删除这些db调用。我不喜欢在视图中调用模型的数据库方法,因为它只是在模板文件中编写SQL的一种更好的方法!如果可以,将它们放在控制器操作中并将它们传递给视图:
# Your controller action
@total_count = ExamQuestion.total_count(exam_id, topic_id)
@correct_count = ExamQuestion.correct_count(exam_id,topic_id)
#...
最后,不要将exam_id和topic_id传递给助手,只需传递@total_count
和@correct_count
即可。好的和小帮手现在:)