2012-12-10 67 views
0

我有四個型號N + 1與軌道關係與活動記錄?

  1. 集團
  2. 報告
  3. 評論
  4. 用戶

組=>的has_many =>報告

報告=>的has_many =>評論

註釋=> belongs_to的=>用戶

當我想顯示一組我這樣做

<%= @group.name %> 
<%= @group.reports.includes(:comments).each do |report| %> 
     <%= report.name %> 
     <% report.comments.each do |comment| %> 
      <%= comment.name %> 
      <%= comment.user.name %> 
     <% end %> 
<% end %> 

什麼是解決在這種情況下,N + 1查詢問題的最佳途徑?

回答

3

也許

@group.reports.includes(:comments => :user).each do |report|