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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:05:47+00:00 2026-05-19T11:05:47+00:00

I am quite new to mongoDB and I’m just using it for a new

  • 0

I am quite new to mongoDB and I’m just using it for a new rails project I’m working on.
What I wanted to do now is, having an (embedded) document reference another collection, but with different roles.

Somehow this doesn’t seem to work. I know, that I can’t define a relation from an external collection to my embedded one. So in my case, I cannot acces the Locations from my User and I’m fine with that. Still, I need to define the referenced_in :location part, because otherwise I get an error message undefined method 'name' for nil:NilClass when I try to add a new Location to the Place.

So that’s just the preface. My actual problem is, that these roles thing doesn’t seem to work. When I create a new Location like that in a Place locations.create(:user_a => some_user), then also user_b is set to that user.

Is there any way to make this working? Or am I doing something wrong?

class Place
  include Mongoid::Document
  embeds_many :locations
end

class Location
  include Mongoid::Document
  embedded_in :place, :inverse_of => :locations
  references_one :user_a, :class_name => "User" #, :stored_as => :array
  references_one :user_b, :class_name => "User" #, :stored_as => :array
end

class User
  include Mongoid::Document
  referenced_in :location, :inverse_of => :dropper
  referenced_in :location, :inverse_of => :picker
end
  • 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-19T11:05:47+00:00Added an answer on May 19, 2026 at 11:05 am

    I think you have to disambiguate the two location associations (because the reference is stored on the referenced_in side):

    class User
      include Mongoid::Document
      referenced_in :location_a, :inverse_of => :user_a, :class_name => "Location"
      referenced_in :location_b, :inverse_of => :user_b, :class_name => "Location"
    end
    

    I can’t help feeling that there may be a better way to do this though…

    Is this a better approach?

    class Location
      include Mongoid::Document
      embedded_in :place, :inverse_of => :locations
      referenced_in :user_a, :class_name => "User"
      referenced_in :user_b, :class_name => "User"
    end
    
    class User
      include Mongoid::Document
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Being quite new to rails and currently building a project, i'm begining to be
I am quite new to mongoid and rails. So I have some troubles to
I am quite new with Django, and I was wondering if I could document
I am quite new to django, and I have built a small page using
Im quite new to XSLT and looking at the decimal-format function im still having
This is just for my weekend project/study, I am very new to Sinatra and
I'm using the Express framework for my node application. I'm quite new to it
I'm new to MongoDB, and went with MongoMapper for some associations help. I'm quite
Quite new to Ruby on Rails and I am stuck on probably an easy
Im quite new to NHibernate and I am having a question regarding the Mapping

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.