Ruby on Rails Monday, September 29, 2014

You would probably want to make a counter cache for tasks on the project (you can find out more here: http://api.rubyonrails.org/v4.1.1/classes/ActiveRecord/Associations/ClassMethods.html#method-i-belongs_to), and additional when retrieving tasks from the database, when you know you will need their project data, use eager loading (more here: http://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations).

Michał.


On 30.09.2014 05:15, Austin York wrote:
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