缓存动态Acts_as_tree类别列表

发布于 2024-12-07 12:01:49 字数 1708 浏览 0 评论 0原文

我有一个使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见终念 2024-12-14 12:01:49

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文