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 4537964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T14:44:48+00:00 2026-05-21T14:44:48+00:00

My goal is to print a json document with the structure described in the

  • 0

My goal is to print a json document with the structure described in the chosen answer on this thread. I only have one level of sub categories (2 total, including root) so it should be a little bit easier than the problem there. The problem I’m having is with efficiency. With only ~20 root categories the recursion is causing my script to act very slowly. My controller looks like this:

def categories
    render :text => "var categories = #{Category.main.map { |c| c.with_children }.to_json};"
end

And the respective method on the Category model:

def with_children
    {
        :name => self.name, 
        :id => self.id, 
        :parent_id => self.parent_id, 
        :children => self.children.blank? ? nil : self.children
    }
end

Can anyone offer some advice to making this process more efficient? Maybe eager loading or something?

  • 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-21T14:44:49+00:00Added an answer on May 21, 2026 at 2:44 pm

    ok, I’m assuming that “main” is a named scope of Category and that children is a has_many association.
    You could add another scope to category, one that retrieves the children of all the categories in one query… Something like:

        scope :with_children, includes(:children)
    

    This would cause that instead of making one query per category to retrieve the children it will make only one query to retrieve the children of all the categories you are loading.

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

Sidebar

Related Questions

goal: I have the string 1234432144 I want to only replace the first 2
My goal is to be able to use $obj like this: print $obj->hello() .
I'm new to python, this is a class that I have class Goal: def
Goal Pass images generated by one process efficiently and at very high speed to
Goal : After user saves data to my mysql DB, a JSON teaser of
I have a set of data that looks like this when using print_r($var) :
I have MySQL database, where I store the following BLOB (which contains JSON object)
My goal is to print *** if the square root is negative. But I
I have need to print indented template names for debugging purposes. For example, instead
I have a Python server script and a Java client application and the goal

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.