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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:14:02+00:00 2026-05-25T22:14:02+00:00

Model description: User, Widget, Purchase User has_many :purchases has_many :widgets, :through => :purchases Widget

  • 0

Model description:

User, Widget, Purchase

User
has_many :purchases
has_many :widgets, :through => :purchases

Widget
has_many :purchases
has_many :users, :through => :purchases

Purchase
belongs_to :user
belongs_to :widget

Hope that makes sense and is correct for the below.

Right, in my controller, I have current_user.

I wish to render a nested JSON response for use eventually with jquery templates.

Like this would be ideal:

{
    "purchases": [
        {
            "quantity": 200,
            "price": "1.0",
            "widget": {
                "name": "Fantastic Widget",
                "size": "Large"
            }
        },
        {
            "quantity": 300,
            "price": "3.0",
            "widget": {
                "name": "Awesome Widget",
                "size": "Medium"
            }
        }
    ]
}

This:

render :json => current_user.to_json(:include => [:purchases, :widgets])

Will render some current_user details (not really relevant) and then purchases and widgets at the same level.

This works (outputs some current user details but thats not my main gripe at the moment):

render :json =>  current_user.to_json({:include => :purchases })

But obviously only outputs purchase data. I cannot get a nested include to work (should it work?) even after looking at this sample:

konata.to_json(:include => { :posts => {
                                     :include => { :comments => {
                                                   :only => :body } },
                                     :only => :title } })

From here and this existing stackoverflow question.

So I’ve just got myself thoroughly confused. Help 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-25T22:14:03+00:00Added an answer on May 25, 2026 at 10:14 pm

    I would look into utilizing the as_json method in your models to get the desired output. Adding the following to your models should get you going in the right direction.

    #users model
    def as_json(options={})
       {:purchases => self.purchases}
    end
    
    #purchases model
    def as_json(options={})
       {:quantity: self.quantity,
       :price: self.price,
       :widget: self.widget}
    end
    
    #widgets model
    def as_json(options={})
       {:name:self.name,
       :size: self.size}
    end
    

    Once you’ve added these you’d simply user to_json on your user instance and it should output correctly.

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

Sidebar

Related Questions

I have a model User, that has_many belongings (a belonging belongs_to a user). I
I have a User and Group model. User has_many Groups and Group belongs_to User
My models: class Order(models.Model): ordered_by = models.ForeignKey(User) reasons = models.ManyToManyField(Reason) class Reason(models.Model): description =
problem description: This model works fine with one user at a time. As soon
I have this model: class UserProfile(models.Model): (UserProfile description) user = models.ForeignKey(User) image = models.ImageField(upload_to=upload_to)
I've created a simple Project model with four attributes: Project name:string description:text complete:boolean user_id:integer
I have the following validation in a model: validates_length_of :description, :minimum => 2, :on
in a Model I have a CharField with choices: class MyModel(models.Model): THE_CHOICES=( ('val',_(u'Value Description')),
I have a model item public class EntryInputModel { ... [Required(ErrorMessage = Description is
Consider for instance my following database model. UserTable(Id, Username) TextContentTable(Id, Name, Description, CreatedByUserId, ModifiedByUserId,

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.