Rails 2.3 中的多查询日志洪水
我正在使用 acts_as_category 插件来管理我的类别逻辑。网站的几乎每个页面都会显示类别和子类别树:
<div id="categories_">
<% Category.roots.each do |category| %>
<h3><%= category.name %></h3>
<div>
<ul class="subcat">
<% category.children.each do |subcategory| %>
<li><%= link_to subcategory.name, "/category/#{subcategory.to_param}" %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
它会生成日志文件中显示的大量 SQL 查询,有没有办法在单个查询中获取多行?
编辑:
我尝试重写类别模型中的根范围,得到相同的结果:
named_scope :roots,
:conditions => ["((hidden IS NULL OR hidden = 0) AND (parent_id IS NULL))"],
:include => :children
编辑:
看来问题出在插件上,作者在这里解释了(http://goo.gl/MwRSJ)为什么无法更改。 对于管理我的类别还有其他建议吗?我使用过acts_as_tree,但它似乎有点过时了。谢谢!
I'm using acts_as_category plugin to manage my categories logic. In almost every page of the site the categories and sub-categories tree is shown:
<div id="categories_">
<% Category.roots.each do |category| %>
<h3><%= category.name %></h3>
<div>
<ul class="subcat">
<% category.children.each do |subcategory| %>
<li><%= link_to subcategory.name, "/category/#{subcategory.to_param}" %></li>
<% end %>
</ul>
</div>
<% end %>
</div>
It produces a lot of SQL queries shown in the log file, is there a way to fetch multiple rows in a single query?
EDIT:
I tried rewriting the roots scope in Category model with the same results:
named_scope :roots,
:conditions => ["((hidden IS NULL OR hidden = 0) AND (parent_id IS NULL))"],
:include => :children
EDIT:
It seems that the problem is with the plugin, and the author explained here (http://goo.gl/MwRSJ) why it can't be changed. Any other suggestion for managing my Categories?. I've used acts_as_tree but it seem a bit outdated. Thanks!.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您看到的是 N+1 查询问题,该问题由 急切加载关联。
What you're seeing is the N+1 query problem which is solved by eager loading associations.