In a todo list-style app, I have the following ActiveRecord model method: class Task < ActiveRecord::Base # ... def project_name project.tasks.length > 0 ? "#{project.name} - #{name}" : project.name end end The idea is to provide additional project information if there are one or more tasks on the project. However, when invoked regularly on views this creates performance concerns (especially with a growing data set). What is the best way to optimize this query so that it doesn't create N+1 query type issues when invoked from "each" loops in the view? (Feel free to post answers on StackOverflow <http://stackoverflow.com/questions/26112070/how-to-avoid-n1-queries-in-rails-each-loops> ). -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/0c714b76-d9d0-4eee-9954-1c50779931bd%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.