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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:45:05+00:00 2026-05-31T03:45:05+00:00

I have two models. I am trying to query a model for how many

  • 0

I have two models. I am trying to query a model for how many relationships it has to the other model. My models are as follows:

# app/models/client.rb
class Client
  include Mongoid::Document
  belongs_to :contact
  ...
end

# app/models/contact.rb
class Contact
  include Mongoid::Document
  has_many :clients
  ...
end

I need to be able to query for the following:

Contacts with NO clients

Contact.where("clients.length == 0")

Contacts with Clients

Contact.where("clients.length > 0")

Can anyone help me with how I would do about this?

  • 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-31T03:45:06+00:00Added an answer on May 31, 2026 at 3:45 am

    Given the following model:

    class Client
      include Mongoid::Document
      belongs_to :contact
      field :name, type: String
    end
    class Contact
      include Mongoid::Document
      has_many :clients
      field :name, type: String
    end
    

    And the following insertions:

    Contact.create(:name => "Bill")
    jill = Contact.create(:name => "Jill")
    jill.clients.create(:name => "Steve")
    

    The following code will do what you need:

    p "Has Clients"
    Contact.any_in(_id: Client.all.distinct("contact_id")).each do |c| 
      p c 
    end
    p "No Clients"
    Contact.not_in(_id: Client.all.distinct("contact_id")).each do |c| 
      p c 
    end
    

    Outputs:

    "Has Clients"
    #<Contact _id: 4f5b04b1e98c373917000002, _type: nil, name: "Jill">
    "No Clients"
    #<Contact _id: 4f5b04b1e98c373917000001, _type: nil, name: "Bill">
    

    Full gist:
    https://gist.github.com/2010817

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

Sidebar

Related Questions

I have two models, one is Group, the other Item. A group has many
I have two models: class Studio(models.Model): name = models.CharField(Studio, max_length=30, unique=True) class Film(models.Model): studio
I have two models: Play and PlayParticipant , defined (in part) as: class PlayParticipant(models.Model):
I have a two models: class Category(models.Model): pass class Item(models.Model): cat = models.ForeignKey(Category) I
I have two models. Comment and his Subcomments: class Comment(models.Model): .... author = models.CharField(max_length=80)
I have two models Jobs and Questions. A job has many questions and questions
I have two models: Activity and Place. The Activity model has a ReferenceProperty to
I have two models indexed for searching (User and Item). I'm trying to do
I have two models, Article and Post that both inherit from a base model
I have two models, Room and Image . Image is a generic model that

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.