N + 1的导轨与活跃记录的关系? [英] N+1 in rails relations with active records?
本文介绍了N + 1的导轨与活跃记录的关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有四个型号
- 集团
- 报告
- 注释
- 用户
组=>的has_many =>报告
Group => has_many => Reports
报告=>的has_many =>评论
Report => has_many => Comments
注释=> belongs_to的=>用户
Comment => Belongs_to => User
当我想给一组我做这样的事情
When i want to show a group I do something like
<%= @group.name %>
<%= @group.reports.includes(:comments).each do |report| %>
<%= report.name %>
<% report.comments.each do |comment| %>
<%= comment.name %>
<%= comment.user.name %>
<% end %>
<% end %>
什么是解决这种情况?
What is the best way to solve N+1 Query problems in this case ??
推荐答案
也许
@group.reports.includes(:comments => :user).each do |report|
这篇关于N + 1的导轨与活跃记录的关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文