I have a view which loops through @regions. For each region its countries are displayed.
<% region.countries.each do |country| %>
A new requirement is to sort the countries by some column, which I have a scope for.
<% region.countries.order_alphabetically.each do |country| %>
However I heard that writing logic in views will severely impact the performance. Is it true for this case? Is is possible to pre-sort this in the controller?
P.S. I don’t want to use default_scope because I need to sort it differently in other views.
EDIT: changed title to better reflect my question
Sorry for the late answer. I wanted to see some evidence, so I finally make time to sit down and write a benchmarking comparison of the two: sorting in view and controller demo.
In the page there are many regions, each regions have many countries. The page display all of them, sorting countries by name for each of the region. Run
rake test:benchmarkand the results will be saved in tmp/performance folder. The results of the two are the same, at around 0.0035 per page render.So in summary, calling a sorting scope in view VS in controller makes no difference in performance.