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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:04:02+00:00 2026-05-26T14:04:02+00:00

I have two entities, projects and users. These are modeled in Rails using Mongoid

  • 0

I have two entities, projects and users. These are modeled in Rails using Mongoid with two Document instances, User and Project.

In this system, one user can create one project, but many users can follow many projects. For example, as user_id 1 I’ve created project_id 1. But user_ids 10, 11, 40, and 60 all follow project_id 1. I need to represent a many-to-many relationship between users and projects, and represent a specific user_id as the creator of the project, to assign him editing rights.

Practically speaking, when a user logs-in, he needs to be able to see all projects that he is following, including any that he created, commingled with other projects created by other users. His special creator status wont influence this list at all. When a user looks at a specific project, he needs to be able to view all users following a project, and if he’s a creator, he can add new followers and delete existing ones.

In a RDBMS I would represents this with tables users, projects and a users_projects join table with a flag of is_creator. This would easily let me select which projects a user can see, and which users are followers, including which users are creators, of projects.

Mongoid supports many-to-many relationships, but unlike in an RDBMS there’s no way for me to put a flag on the relationship. Instead, I’m thinking I’ll add a creator field to the projects document, which will contain a link back to an _id field on the users document.

The user->projects relationship might look like this

class User
  has_and_belongs_to_many :projects
end

class Project
  has_and_belongs_to_many: users
end

But I can’t figure out how to map the creator->created_projects relationship. I believe I can reference a user creator in Project like belongs_to :creator, :class_name => 'User' but I’m not sure how to set up the other side.

How best can I model these relationships in Mongoid?

  • 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-26T14:04:03+00:00Added an answer on May 26, 2026 at 2:04 pm

    create an embedded document which holds all followers with their user_id and their username so you won’t have to query the follower’s usernames.

    The benefits:

    • Not a single query to lookup a project’s followers
    • Only a single query to lookup a user’s followed projects

    The downside:

    • If a user changes his name, you’ll have to update all his “followships” but how often
      do you change your name compared to how often you lookup your followed projects 😉

    • If you have many thousand followers per project you may reach the document limit of 16mb

    user.rb

    class User
      # projects this user owns
      has_many :projects
    
      def followed_projects
        Project.where('followers.user_id' => self.id)
      end
    
      # get projects that this user has created and projects he is following
      def related_projects
        Project.any_of({:user_id  => self.id}, {'followers.user_id' => self.id})
      end
    end
    

    project.rb

    class Project
      # creator
      belongs_to :user
    
      embeds_many :followers
    
      # add an index because we're going to query on this
      index 'followers.user_id'
    
      # adds a follower
      # maybe you want to add some validations, preventing duplicate followers
      def add_follower!(user_obj)
        self.followers.create({
          :user       => user_obj,
          :username   => user_obj.username
        })
      end
    
      def remove_follower!(user_obj)
        self.followers.destroy_all(:conditions => {:user_id => user_obj.id})
      end
    
    end
    

    follower.rb

    class Follower
      include Mongoid::Document
      include Mongoid::Timestamps
    
      embedded_in :project
    
      # reference to the real user
      belongs_to :user
    
      # cache the username
      field :username, :type => String
    end
    

    How to work with it:

    @project    = Project.first
    @some_user  = User.last
    
    @project.add_follower!(@some_user)
    
    @some_user.followed_projects
    @some_user.related_projects
    
    @project.followers.each do |c_follower|
      puts "I'm #{c_follower.username} and I'm following this project!"
    end
    
    @all_follower_user_ids = @project.followers.map{|c| c.user_id}
    
    # find a specific follower by user_id 
    @some_follower = @project.followers.where(:user_id => 1234)
    # find a specific follower by username
    @some_follower = @project.followers.where(:username => 'The Dude')
    
    @project.remove_follower!(@some_user)
    

    PS: If you want a simpler solution, you could just embedd an array of ObjectIDs (user_ids) in the project and use the atomic updates $addToSet and $pullAll to add/remove a follower. But you’d need an extra query like User.where(:user_id.in => @project.follower_ids) (assuming the array is called follower_ids) to grab all users and their names 😉

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

Sidebar

Related Questions

In a rails project I have two entities, Users and Institutions, they have a
I have two entities, a User and Role with a one-to-many relationship from user
I have two entities: Project, Employee Employee has primary key {employeeId} + some other
In my project I am using NHibernate/FluentNHibernate, and I am working with two entities,
For example: I have two entities named Project and Todo where a project has
In my project, I have two Core Data entities. One is the grouping (can
Hii, I have two entities with bidirectional association. Project.java class Project{ int project_id; @OneToMany(mappedBy=project)
I have two projects: News.Data Tags.Data Both define Entities . When I try to
I have two entities, User and UserPermission. The User entity contains all your normal
I have two entities, a project and a company. One company has a list

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.