J'ai un blog. Sur ma page d'index, j'insère tous les articles du blog. Pour chaque article de blog, je compte le nombre de commentaires sur ce post. Cela conduit à un problème N + 1. Mes questions se présenter comme suit:Rails SQL COUNT N + 1 inefficacité
SELECT "blog_posts".* FROM "blog_posts" WHERE ("blog_posts"."published" = 't') ORDER BY published_at DESC
SELECT "users".* FROM "users" WHERE ("users"."id" IN (1, 2, 3))
SELECT COUNT(*) FROM "blog_comments" WHERE ("blog_comments".blog_post_id = 10)
SELECT COUNT(*) FROM "blog_comments" WHERE ("blog_comments".blog_post_id = 9)
SELECT COUNT(*) FROM "blog_comments" WHERE ("blog_comments".blog_post_id = 8)
SELECT COUNT(*) FROM "blog_comments" WHERE ("blog_comments".blog_post_id = 2)
SELECT COUNT(*) FROM "blog_comments" WHERE ("blog_comments".blog_post_id = 7)
Est-il possible dans Rails pour inclure le COUNT de la même manière que je les utilisateurs (inclure la ligne de SQL 2)?
+1 C'est génial! –
C'est exactement ce que je cherchais! Merci! – Mike