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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:26:15+00:00 2026-05-25T23:26:15+00:00

Given the following class, how would I allow a node to have a many

  • 0

Given the following class, how would I allow a node to have a many to many relationship with other nodes (such as parent_node and child_nodes)?

class Node
  include MongoMapper::Document

  key :text, String
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-25T23:26:15+00:00Added an answer on May 25, 2026 at 11:26 pm

    Can children have more than one parent? If not, many/belongs_to should work fine:

    class Node
      include MongoMapper::Document
    
      key :text, String
      key :parent_node_id, ObjectId
    
      belongs_to :parent_node, :class_name => 'Node'
      many :child_nodes, :foreign_key => :parent_node_id, :class_name => 'Node'
    end
    

    If nodes can have multiple parents…

    class Node
      include MongoMapper::Document
    
      key :text, String
      key :parent_node_ids, Array, :typecast => 'ObjectId'
    
      many :parent_nodes, :in => :parent_node_ids, :class_name => 'Node'
    
      # inverse of many :in is still forthcoming in MM
      def child_nodes
        Node.where(:parent_node_ids => self.id)
      end
    end
    
    # ... or store an array of child_node_ids instead ...
    
    class Node
      include MongoMapper::Document
    
      key :text, String
      key :child_node_ids, Array, :typecast => 'ObjectId'
    
      many :child_nodes, :in => :child_node_ids, :class_name => 'Node'
    
      # inverse of many :in is still forthcoming in MM
      def parent_nodes
        Node.where(:child_node_ids => self.id)
      end
    end
    

    Is that what you’re looking for?

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

Sidebar

Related Questions

Given the following, I would not expect the compiler to allow multiple attributes that
Given the following class: class A { public List<B> ListB; // etc... } where
Given the following models: class Post(models.Model): title = models.CharField(max_length=200) html = models.TextField() class PostTag(models.Model):
Given the following model: class Project(models.Model): project_name = models.CharField(max_length=255) abstract = models.TextField(blank=True, null=True) full_description
I want to override the tree_id field as following: Given: class Thing(MPTTModel): thing_id =
This is just an example, but given the following model: class Foo(models.model): bar =
Given the following code: final class retVal { int photo_id; } Gson gson =
Given the following multiton: public class Multiton { private static final Multiton[] instances =
Given the following types private class MyTestDummyClassValidationDef : ValidationDef<MyTestDummyClass> { ... } public class
Given the following object: public class Product { string Name {get;} int Quantity {get;}

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.