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

  • Home
  • SEARCH
  • 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 1034507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T14:24:12+00:00 2026-05-16T14:24:12+00:00

I am using Mongoid and have a project and a user model. in the

  • 0

I am using Mongoid and have a project and a user model.
in the Project model, I have a field

class Project
  include Mongoid::Document
  field :name
  field :user_ids, :type => Array
end

class User
  include Mongoid::Document
  field :email
end

I can find all the users belonging to one project, i.e., ‘find this project’s users’

@project  = Project.first # => 'Housework'
User.criteria.id(@project.user_ids) # => ['Bart','Lisa','Maggie']

But I am having a bit trouble finding all the projects belonging to one user, i.e, ‘find this user’s projects’

@user = User.first   # => 'Bart'
Project.where(:user_ids => @user.id) # doesn't work
Project.where(:user_ids.includes => @user.id) # not such method
Project.where(:user_ids => [@user.id]) # doesn't make sense to compare arrays, but tried anyway and doesn't work

I know that you can have another field in the User model to store project_ids, I would gladly do that, but I am just curious, is there a method to be used in finder conditions that works similarly to #includes? in ruby?

  • 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-16T14:24:13+00:00Added an answer on May 16, 2026 at 2:24 pm

    I found a solution to this. it is the all_in finder method

    example:

    Fruit.all[0].colors = ['red','green','blue'] #=> apple
    Fruit.all[1].colors = ['yellow','green']     #=> banana
    
    Fruit.all[2].colors = ['red', 'yellow']      #=> pineapple
    

    To find all fruits that have the color red in their ‘colors’ array field, one can query:

    Fruit.all_in(:colors => ['red'])
    

    =>[apple, pineapple]

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

Sidebar

Related Questions

I have recently been getting my feet wet in MongoDB using Mongoid w/ Rails
In this simplified scenario, I have a model called Post Each post has a
I recently played with MongoDB in Rails using Mongoid . I like the ability
I'm using Rails 3 w/ Mongoid, (so no ActiveRecord). Mongoid uses ActiveModel's to_json method,
I have launch mongodb server: [demas@arch.local.net][~]% mongod --dbpatmongod --dbpath /home/demas/temp/ Mon Apr 19 09:44:18
There is a similar question about 'bson_ext', but it's not work for me. MongoMapper
I am working with Node.js to build a web socket server that uses mongodb.
Not infrequently, one wants to implement the <=> (comparison, or spaceship) operator on a
It is well-known that monoids are stunningly ubiquitous in programing. They are so ubiquitous
I am running some unit test that persist documents into the MongoDb database. For

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.