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:
|
classTask<ActiveRecord::Base
# ...
defproject_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+unsubscr...@googlegroups.com
<mailto:rubyonrails-talk+unsubscr...@googlegroups.com>.
To post to this group, send email to rubyonrails-talk@googlegroups.com
<mailto: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
<https://groups.google.com/d/msgid/rubyonrails-talk/0c714b76-d9d0-4eee-9954-1c50779931bd%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.
--
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+unsubscr...@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/542A4DB3.7020402%40gmail.com.
For more options, visit https://groups.google.com/d/optout.