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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:13:12+00:00 2026-05-26T18:13:12+00:00

Importing the language of graph databases , understand nodes ( represented by circles ),

  • 0

Importing the language of graph databases, understand

  1. nodes (represented by circles),
  2. edges (represented by arrows), and
  3. properties (metadata of nodes / edges)

Graph Database Property Graph

The graphic (courtesy of wikipedia) describes a directed graph.

What’s the best way to model an undirected graph in Rails?

That is to say, a graph where all edges are reciprocal (as in above graphic), and where the properties of each edge are the same regardless of direction (contrary to above graphic).

Let’s assume a default Rails 3 setup using a sql store via ActiveRecord.

A double polymorphic association would create a directed graph, able to model the data described by the above image.

def Edge < ActiveRecord::Base
  belongs_to :head, polymorphic: true
  belongs_to :tail, polymorphic: true
end

class Node < ActiveRecord::Base
  has_many :from, as: :head
  has_many :to, as: :tail
end

class Group < ActiveRecord::Base
  # a Node of Type: Group
  has_many :from, as: :head
  has_many :to, as: :tail
end

Should one extend this model to manage inverse relationships, or is a better model available?


One element of an app may be a graph problem, but it does not mean the app is centered around the problem, that graph transversals must be performed on the data, nor that the dataset is larger than available memory.

  • 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-26T18:13:12+00:00Added an answer on May 26, 2026 at 6:13 pm

    In an undirected graph, the only thing you need to know, is whether a node is connected to another node. And there is no such thing as a direction.

    Simple approach:

    class Node
      has_many :connected_nodes
      has_many :nodes, :through => :connected_nodes
    end
    
    class ConnectedNode
      belongs_to :node
      belongs_to :connected_node, :class_name => 'Node'
    end
    

    This is also called an adjacency list: for each node we can easily get the list of adjacent (connected) nodes.

    A possible problem with this approach: we store the connections twice. A is connected to B and B is connected to A.

    So it seems better normalized to store each connection only once, and then we get really close to your original proposal.

    class Connection
      belongs_to :node1, :class_name => 'Node'
      belongs_to :node2, :clasS_name => 'Node'
    end
    

    Only we do our very best to not impose any order or direction through the naming.

    Retrieving the connected nodes is all the nodes connected to as node1 or as node2, hence effectively disregarding any possible direction.

    In this case you also need to express a validation that a connection with (node1, node2) is unique, but that (node2, node1) is actually the same and cannot be inserted twice.

    My personal choice would be to use the second schema, although maintaining the first solution might be quicker (see also this question).

    I also found a very interesting article where the author explains how graphs can be stored in the database. Very profound, but more database centric.

    Hope this helps.

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

Sidebar

Related Questions

I came across a C# language feature today courtesy of ReSharper, the ?? operator.
I am looking for an efficient eigensolver ( language not important, although I would
I need GUI control for audio file presentation. The language is not very important
When importing numbers from a csv file, I need to convert them to floats
After importing content from a legacy website using the Feeds module, I'm left with
I'm importing some data from a CSV file, and numbers that are larger than
I am importing the CreateICeeFileGen() function from the unmanaged DLL mscorpe.dll in a C#
So I am importing some images stored in SQL image columns, and I need
We are importing from CSV to SQL. To do so, we are reading the
I am importing csv to db using bulk insert. It is the comma delimited

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.