【发布时间】:2011-12-14 18:16:05
【问题描述】:
在下面的 sn-p 中,我在视图中进行了大量的数据库操作。 (.where 和两个 each 循环)。将此代码重构到视图之外的最佳方法是什么?
在视图中:index.html.erb
<%- @lesson.sections.each do |section| -%>
<%- section_correlations = section.correlations.where(:grade => 4) %>
<%- unless section_correlations.blank? -%>
<h3><%= section.full_title %></h3>
<%- section_correlations.each do |correlation| -%>
<%= correlation.description %>
<%- end -%>
<%- end -%>
<%- end -%>
【问题讨论】:
标签: ruby-on-rails ruby-on-rails-3 activerecord refactoring where