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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:06+00:00 2026-05-23T15:08:06+00:00

Consider a typical relational structure: Answer has many comments. comments belongs to a user,

  • 0

Consider a typical relational structure:

Answer has many comments. comments belongs to a user, and have many likers

answers.to_json(:include => {:comments => {:include => :user, :likers}})

If i call to_json , it will perform the database retrievals one by one for individual user in individual comments.

The much more efficient solution would be to put the required ids for each type in an array, do 3 database calls to retrieve them, put it into a hash, and construct the json according to the hash.

This seems like a pretty common use case. I am thinking of writing my own recursive function for this, but if anyone can point me to something that has already been done, that will be great!

** i am using mongoid / mongodb, i am not sure if it works the same way in active record.

  • 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-23T15:08:06+00:00Added an answer on May 23, 2026 at 3:08 pm

    May be you can try mongoid eager loading, then you can query mongo just like AR eager loading

     Answer.includes(:comments, :likers)
    

    The patch is still not included in the mongoid master branch, but you can download it as a independednt gem from here

    Read this pull request for more info

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

Sidebar

Related Questions

Consider this: One mySQL database that has tables and rows and data within it.
Consider a simple setup: // _conn is the OdbcConnection with a MySQL-Server (MySQL-Connector 3.51)
Consider the following Scala case class: case class WideLoad(a: String, b: Int, c: Float,
Consider following snippet code for xml. <rootnode> <child id=child1 ><![CDATA[child 1]]></child> <child id=child2 ><![CDATA[child
Consider the following function, which does not work in Python, but I will use
Consider I modify the way files are stored in a system, where every file
Consider the following Enum and a corrsponding nullable field of that type enum PossibleOptions
Consider the following struct to be sent over TCP to an unmanaged dll [StructLayout(LayoutKind.Sequential,
I'm currently developing the foundation of a an application, and looking for ways to
I am wondering how to ensure that an employee cannot access information from another

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.