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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:33:25+00:00 2026-06-09T16:33:25+00:00

I have a data structure that I’m looking to model and persist in rails.

  • 0

I have a data structure that I’m looking to model and persist in rails. It’s self-referential and would look a lot like a hierarchical tree however instead of having just one parent each node could have multiple parents and multiple children.

It might look something like this (all nodes are the same model):

                       Foo            Bonk
                     |    |          |    |
                    Bar   Baz      Foo   Bork 
                   |   |
                 Baz   Bork

Where all of the nodes with the same name are actually the same node.

I also want to find all instances of the model that contain a certain “ingredient.”

The closest thing I found was https://github.com/mceachen/closure_tree but as far as I can tell each node only has one parent.

I’m ideally looking for a pattern to implement with ActiveRecord, but will consider any solution.

  • 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-06-09T16:33:26+00:00Added an answer on June 9, 2026 at 4:33 pm

    For each node having only one parent, this is a good way to do it:

    has_many :children, :class_name => "Node", :foreign_key => "parent_id"
    belongs_to :parent, :class_name => "Node", :foreign_key => "parent_id"
    

    As you can tell, its an ActiveRecord model with a field named parent_id pointing to the parent node.

    For multiple parents, this can be extended to create a many-to-many self referential join.

    has_and_belongs_to_many :children, :class_name => "Node",
        :foreign_key => "node_id",
        :association_foreign_key => "children_id",
        :join_table => "children_nodes"
    

    Create the join table, now you should be able to do Node.children and Node.nodes for parents.
    Note: The second part has not been tested, but let me know if it doesn’t work.

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

Sidebar

Related Questions

I have a data structure that looks like this: Model Place primary key id
I have a complex Clojure data structure that I would like to serialize -
I have a data structure that looks like this (simplified version) class A(models.Model): a
I have a JSON-like data structure (which I don't want to change) that is
I have the need to use a Stack-like data structure for a program that
I have a data structure that looks like this: data =[ {'key_1': { 'calc1':
I have a custom read-only data structure that I need to transverse. I would
I have a data structure that looks like [(hello, 12), (yo, 30)] . How
I currently have an array data structure that I iterate over like this, calling
I have a data-structure (in plist) that looks something like this: What i have

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.