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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T15:51:10+00:00 2026-05-26T15:51:10+00:00

I have a model like this class Parent < ActiveRecord::Base :has_many :kids end class

  • 0

I have a model like this

class Parent < ActiveRecord::Base
  :has_many :kids
end

class Kid < ActiveRecord::Base
  :has_many :grandkids
  :belongs_to :parent
end

I can generate json like this:

the_parent.to_json( :methods => [:kids] )

=> { “parent” : { “kids” : [ { “kid” : { “name” => “kid0” …. and so on. Just what I want. Each object looks like a hash with a single key – which is the model name – and the value is an attribute hash. Great.

But I get into trouble when I try to serialize the whole tree, like this:

the_parent.to_json( :include => { :kids => { :include => :grandkids } } )

=> { “parent” : { “kids” : [ { “name” => “kid0” …
which is missing the model names in the “kids” array. The same thing happens at the next level with the grandkids. I’m going to parse this somewhere else, and it would help to have certainty about the object name (as opposed to relying on convention using the relationship name). The docs advertise this:

ActiveRecord::Base.include_root_in_json = true

Which I found it to have no effect. My guess is the different behavior has something to do with the difference between the :method and :include options, but I can’t wrangle the :method syntax to get the nesting I need, and I’m not sure if that will work even if it compiles.

Any ideas? Thanks, Dan

  • 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-26T15:51:11+00:00Added an answer on May 26, 2026 at 3:51 pm

    As a workaround, I’m overriding to_json in my model like this:

    def to_json(args)
        super( :methods => [:kids] )
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails model that looks something like this: class Recipe < ActiveRecord::Base
I have a model called Category which looks like this: class Category < ActiveRecord::Base
I have this Rails model: (Parameters removed for clarity) class Folder < ActiveRecord::Base belongs_to
In my rails app, have a tree-like model like this: class File belongs_to :parent,
I have a Django model which looks like this: class MyModel(models.Model): parent = models.ForeignKey(ParentModel)
I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end
Let's say I have a model like this class Foo(db.Model): id = db.StringProperty() bar
I have a model class like this: class Note(models.Model): author = models.ForeignKey(User, related_name='notes') content
I have a simple model like this one: class Artist(models.Model): surname = models.CharField(max_length=200) name
I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A

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.