Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6640155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:38:24+00:00 2026-05-25T23:38:24+00:00

I have a Category model which uses acts_as_tree class Category < ActiveRecord::Base acts_as_tree :order=>name

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:38:24+00:00Added an answer on May 25, 2026 at 11:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have a Model Category which has name:string categoryId:integer parent_id:integer the categoryId doesnt matter
i have two models: class Category has many :jobs end class Job belongs_to :category
I have a main page which uses a ViewModel I have created: public class
I have a Category model, and a Project model, which contains a ForeignKey to
In my code I have a Category model which has many subcategories, and the
I have a model category which contains a field called category.parent_id which is used
I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) I
I have category model, in which I want to load some default data. How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.