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

The Archive Base Latest Questions

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

I have the following models in my app and I am using mongoid User,

  • 0

I have the following models in my app and I am using mongoid

User, Account, Office and Leads
A user has many accounts and an account belongs to a user
An account has many offices and an office belongs to an account
An office has many leads and a lead belongs to an office

Now I want to query such that I want to get the leads belonging to the offices of accounts of a user, like `

@accounts = Account.where(:user => user)

Now I want to get the leads of the offices belonging to @accounts. Is there a decent way to accomplish it? or I have to iterate over each account and get the offices belonging to that account and then ultimately find the leads.

I could persist the account information in the leads itself so that they contain both the account and office information so that I can query in one shot. But is that the right way?

Suggestions?

  • 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:13:08+00:00Added an answer on May 25, 2026 at 10:13 pm

    If these are “contains” relationships consider embedding rather than linking. For example if all leads belong to one office you could embed them therein:

    { office:”…”, leads : [ { … }, { … } ] }

    Likewise the offices could be embedded in an account document. And so forth.

    However whether the above makes sense depends on the operations you plan to do (if fully embedded a query such as “give me all leads” if more work). Also there is a maximum document size of 16MB.

    Likely some combination of embedding and linking is appropriate. When linking you have to do it client side as you mentioned. See the schema design and “reaching into objects” pages on mongodb.org.

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

Sidebar

Related Questions

I am using Rails 3.2. I have following models: Blog Comment User class Blog
I'm using Django-nonrel on Google App Engine and have the following models (they are
I have the following models in my Django app. How can I from the
In my GWT app I have the following model class: import com.google.gwt.user.client.rpc.IsSerializable; public class
I have the following models: class Person < ActiveRecord::Base has_many :accounts, :through => :account_holders
I have the following models in my app (only showing the related scopes): class
I have the following Models set up in my app: Lesson Student Evaluation The
In my Django App I have the following model: class SuperCategory(models.Model): name = models.CharField(max_length=100,)
I have a rails app with two models, visits and users. A visit has
I have a ruby app that uses ActiveRecord. I have the following models module

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.