缓存动态Acts_as_tree类别列表
我有一个使用acts_as_tree的类别模型
class Category < ActiveRecord::Base
acts_as_tree :order=>"name"
end
当我显示我的类别树时,我使用递归,并且每次都会生成几乎相同的部分(除了某些复选框被选中或不选中),需要大量的数据库调用。
我想缓存它,但目前我唯一能想到的就是将 Category.all 转储到新的非 ActiveRecord 数据结构中以减少调用次数。有更好的办法吗?
索引.html.erb
<%= render :partial=> "/categories/category_checkboxes", :locals=>{:select_categories=>@categories_ids} %>
_category_checkboxes.html.erb
<% @categories.each do |category| %>
<h3><a href="#"><%=category.name%></a></h3>
<div>
<% category_children = category.children %>
<%= render :partial => "/categories/category_checkbox_children",
:locals => { :child_categories => category_children,
:chk_class=>chk_class,
:select_categories=>select_categories } unless category_children.empty? %>
</div>
<% end %>
_category_checkboxes_children.html.erb
<ul>
<% child_categories.each do |category| %>
<li class= "category_check_box">
<%=check_box_tag("category#{category.id}", 1, select_categories.index(category.id)%>
<%=label_tag("category#{category.id}" ,"#{category.name}")%>
<%= render :partial => "/categories/category_checkbox_children", :locals => {
:child_categories => category.children,
:select_categories=>select_categories} unless category_children.empty? %>
<% end %>
</li>
</ul>
I have a Category model which uses acts_as_tree
class Category < ActiveRecord::Base
acts_as_tree :order=>"name"
end
When I display my category tree I use recursion, and an almost identical partial is generated each time (apart from some checkboxes being checked or not), requiring a large number of database calls.
I would like to cache this, but at the minute the only thing I can think of is by dumping Category.all in to a new non-ActiveRecord data structure to reduce the number of calls. Is there a better way?
index.html.erb
<%= render :partial=> "/categories/category_checkboxes", :locals=>{:select_categories=>@categories_ids} %>
_category_checkboxes.html.erb
<% @categories.each do |category| %>
<h3><a href="#"><%=category.name%></a></h3>
<div>
<% category_children = category.children %>
<%= render :partial => "/categories/category_checkbox_children",
:locals => { :child_categories => category_children,
:chk_class=>chk_class,
:select_categories=>select_categories } unless category_children.empty? %>
</div>
<% end %>
_category_checkboxes_children.html.erb
<ul>
<% child_categories.each do |category| %>
<li class= "category_check_box">
<%=check_box_tag("category#{category.id}", 1, select_categories.index(category.id)%>
<%=label_tag("category#{category.id}" ,"#{category.name}")%>
<%= render :partial => "/categories/category_checkbox_children", :locals => {
:child_categories => category.children,
:select_categories=>select_categories} unless category_children.empty? %>
<% end %>
</li>
</ul>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
acts_as_tree
gem 已经过时了。最后一个版本 (0.1.1) 是 2010 年 2 月发布的,其功能相当有限。我建议您看一下 ancestry,这是一个提供类似功能的 gem,并且添加了更多功能。具体来说,请查看按深度选择节点部分。
The
acts_as_tree
gem is rather out of date. The last version (0.1.1) is from February 2010, and its functionality pretty limited.I recommend you take a look at ancestry, a gem that provides similar functionality, and has added much more. Specifically, take a look at the section on Selecting nodes by depth.