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

The Archive Base Latest Questions

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

I figured out how to include the children in a to_json result by creating

  • 0

I figured out how to include the children in a to_json result by creating an as_json method in the folder model.

def as_json(options={ })
  super(
          options.merge(
                  :include => {
                          :children => { }
                  }
          )
  )
end

The above code gives me a list of the children, but what I want is to include the count not the list of children. I also want to filter it to only “Active” children.

I can’t seem to figure out an efficient way to do this.

I’m using the below code to return a list of folders.

def index
  @folders = Folder.all(:order => "Name")

  respond_with(@folders) do |format|
    format.jsonp { render :json => @folders, :callback => params[:callback] }
  end
end

Finally, I want to make sure the count comes back in all results not just json. json is the most important but I do want to make sure it’s in the others as well.

I thought I would be able to add the count to each of the folders returned by @folders after the query by using .size. However, that doesn’t seem to work…

Next I started looking into adding a method to the Folder model such as…

def child_count
  write_attribute(:child_count, children.size)
end

But after looking at it I realized that’s not really going to work…

So now I’m looking into :select to do some sql magic… but I really don’t want to go that route if there’s a cleaner way.

Tips appreciated!

  • 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-25T02:24:48+00:00Added an answer on May 25, 2026 at 2:24 am

    Just add a child_count method that returns the number of children and then use the :methods option for as_json:

    def as_json(options = { })
        super(options.merge(:methods => :child_count))
    end
    

    You might want to be a bit more careful with that merge though, if there is a :methods already in options you’ll overwrite it; something like this might serve you better than a straight merge:

    def add_child_count(options)
        options[:methods] = [ options[:methods], :child_count ].flatten.compact.uniq
        options
    end
    
    def as_json(options = { })
        super(add_child_count(options))
    end
    

    For XML, you do pretty much the same thing with to_xml instead of as_json.

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

Sidebar

Related Questions

I can't figure out the problem in this: #include<stdio.h> int main() { int a,b,count
I'm trying to figure out how to limit my child dataset to only include
How can I figure out the size of a file, in bytes? #include <stdio.h>
I figured out through trial n error how to link an unmanaged static library
I figured out that CUDA does not work in 64bit mode on my mac
I've figured out how to set VC++ to compile code into a .lib file
So I figured out that by adding the ResetWebServer=FALSE attribute to the solution manifest
After a while I figured out I could bind Shark to the process of
I haven't figured out the correct programming architecture for: -WPF Application -Each build will
I've figured out how to assign a rotation value (element.RenderTransform = new RotateTransform(x)), but

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.