Tengo este esquema: Publicar pertenece_ a Categoría y Categoría has_many Mensaje. Post y por categoría son globalizar con globalize3 joyaRails 3 incluye las traducciones globalize3 activerecord
class Post < ActiveRecord::Base
belongs_to :category
translates :title, :excerpt, :desC# globalize3
end
class Category < ActiveRecord::Base
has_many :posts
translates :name # globalize3
end
En mi PostsController consigo todos los mensajes con esta línea de código:
def index
@posts = Post.includes([:translations, {:category => :translations}])
end
El problema es que no tengo n + 1 problema de consulta con categoría traducciones tabla:
Post Load (0.3ms) SELECT "posts".* FROM "posts"
Post::Translation Load (0.3ms) SELECT "post_translations".* FROM "post_translations" WHERE ("post_translations".post_id IN (2,3,4))
# START n+1 query block
Category Load (1.9ms) SELECT "categories".* FROM "categories" WHERE ("categories"."id" IN (9,12,11))
Category::Translation Load (0.4ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 9) AND ("category_translations"."locale" IN ('it'))
CACHE (0.0ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 9) AND ("category_translations"."locale" IN ('it'))
Category::Translation Load (0.2ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 12) AND ("category_translations"."locale" IN ('it'))
CACHE (0.0ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 12) AND ("category_translations"."locale" IN ('it'))
Category::Translation Load (0.2ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 11) AND ("category_translations"."locale" IN ('it'))
CACHE (0.0ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id = 11) AND ("category_translations"."locale" IN ('it'))
# END n+1 query block
Category::Translation Load (0.5ms) SELECT "category_translations".* FROM "category_translations" WHERE ("category_translations".category_id IN (9,12,11))
¿Cómo puedo resolver este problema de consulta n + 1?