Actions_as_taggable_on: how to optimize a request?

I am using acts_as_taggable_on in my current Rails project. On one overview page, I show an index of objects with tags associated with them. I am using the following code:

 class Project < ActiveRecord::Base acts_as_taggable_on :categories end class ProjectsController < ApplicationController def index @projects = Project.all end end # in the view <% @projects.each do |p| %> <%= p.name %> <% p.category_list.each do |t| %> <%= t %> <% end %> <% end %> 

Everything works as expected. However, if I show 20 projects, acts_as_taggable_on runs 20 requests to retrieve the associated tags.

How to enable tag loading in db source request?

Thank you for your time.

+7
source share
3 answers

Try

@projects = Project.includes (: categories) .all

+9
source

I agree with Jan Dreyuyak, tremendous performance with

 Download.includes(:tags).all 

and in the views:

 download.tags.map {|t| link_to t, t.name}.join(', ') 

But still too slow.

Any other idea?

+2
source

Use this:

Post.includes (: tags) .all

and then:

post.tags.collect {| t | t.name}

0
source

All Articles