Commentテーブルをクエリするときに sをeager-loadしようとしていActivityます。
# Activity (basic FB-style news feed)
# user_id
# primary_source_id (polymorphic object, like "Post", that can have comments)
# primary_source_type
# Comment (acts as commentable w/ threading gem)
# user_id
# commentable_id
# commentable_type
# WHAT GOES HERE?!
# How do I eager-load comments?
activities = Activity.includes(???).joins(???)
# Display code
activities.each do |activity|
render ... # render activity
activity.root_comments.each do |comment|
render ... # render activity's comments
end
end
Activitys をループし、それぞれprimary_source( a などPost) とそのsを取得して、ページをレンダリングしますComment。現在、primary_sourceは熱心にロードされていますが、Commentはそうではありません。各ループがCommentテーブルにヒットします。これは私にとって大きなパフォーマンス ヒットであり、表示する s の数に比例してスケーリングしActivityます。
を熱心にロードするにはどうすればよいCommentですか?