In the below snippet, I’m doing a lot of database manipulation in the view. (The .where, and two each loops). What is the best way to refactor this code out of the view?
In the view: 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 -%>
in the Section model file you could add the following method
and in your lesson model:
then in your view: