Ruby on Rails Monday, September 29, 2014

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).

--
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@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
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.

No comments:

Post a Comment