次のような例の場合で、postに対するcategoryがN+1の問題が生じてしまいます。
こういうケースにおいて、joinなりincludeなどで解消することはできないのでしょうか?
Userに対してはpostのincludeはできますが、categoryに対してはできませんでした。
設計から見直す必要があるのでしょうか?
よろしくお願いします。
controller
class UserController < ApplicationController @users = User.all.include(:posts) end
view
- @users.each do |user| - user.posts.each do |post| = post.name = post.category.name <==こういうの = post.like_count
modelのリレーション
User.has_many :posts Post.has_one :category Post.belongs_to :user Category.belongs_to :post
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/05/29 11:47
2016/05/29 12:08
2016/05/29 12:56
2016/05/30 03:59