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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:38:29+00:00 2026-06-05T04:38:29+00:00

I am wondering how I can make this work. Assuming I have a model

  • 0

I am wondering how I can make this work. Assuming I have a model of Project that belongs_to a Client. I want to output those particular projects to JSON, but they need to be grouped by their client. I thought this would work:

@projects.group_by(&:client).to_json

That ALMOST works, except the to_json method isn’t calling .to_json on the client, so for the client you get this in your JSON:

"#<Client:0x1051d4fb0>":[{ "project":{"name":"My Only Project", "client_id":1}}]

But I really need it like this:

{"client":{"name":"Home Workers Unite", "id":1, "projects":[{"name":"My Only Project","client_id":1}]}}

Any help is 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-06-05T04:38:31+00:00Added an answer on June 5, 2026 at 4:38 am

    As already told by @LarryK, the json you want as out put is not a valid json. In json keys can only be strings unlike ruby which can has objects as keys. Also, I would suggest that the json response be more like following:

    {clients: [
        {
          "name":"Home Workers Unite", 
          "id":1,
          "projects": [
            {"name":"My First Project", "id":1},
            {"name":"My Second Project", "id":2}
          ]
        }
      ]
    }
    

    You already have projects available with you. You also have the code to group them by client, but a simple to_json on the resulting hash would not be suitable here. You would need to manipulate the grouped response further to be able to directly use to_json:

    results = {clients: []}
    @projects.group_by(&:client).each do |client, projects|
      results[:clients] << {
        id: client.id,
        name: client.name
        projects: projects
      } 
    end
    results.to_json
    

    This is ruby/rails version of the solution. However if you have a json heavy application, I would suggest using some gem(jbuilder, rabl, or any other) to create the desired responses.

    PS: code is not tested

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

Sidebar

Related Questions

I am new to rails and was wondering how I can make this work.
just wondering if someone can help me make this code work properly and be
i was wondering if anyone can tell me how to make this script work.
I am basically just wondering how can I make this while loop repeat continuously
Just wondering how I can make my app open automatically at login, but make
I was wondering how I can make a script load powerpoint file, advance slides
Hey so I'm wondering how can I make AtomicInteger a two dimensional array, From
Hey guys I'm using python and I was just wondering how we can make
I'm using XNA. I'm wondering if on the menu screen I can make the
I was wondering, how can I make jquery UI not to adds any class

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.