Essentail Rails Design Pattern

Write Good Rails Code

Query in Loop

ABUSE: SQL Query in LOOP

1
2
3
<% Post.limit(10).each do |post| %>
<%= post.categories.map{ |c| "<span>#{c.name}</span>"} =%>
<% end %>
  • Some operation is very EXPENSIVE

Solution

  • cache expensive operation
    • counter_cache, memorize, cache
  • includes
  • find_in_batch

Comments