Directed Acyclic graph implementation in Ruby on Rails I have an implementation for a graph node class that I'd like to have function as a directed acyclic graph. 

The associations are roughly as follows

class Node
  has_many :parents, 
           class_name: 'Edge',
           foreign_key: :child_id

  has_many :children,
           class_name: 'Edge',
           foreign_key: :parent_id
end

class Edge
  belongs_to :parent, class_name: 'Node'
  belongs_to :child, class_name: 'Node'

  has_many :edge_properties
end


Me and a co-worker of mine had a discussion about the proper way to do this has_many relationship. The rails paradigm seems to dictate that node.parents should return a Node::ActiveRecord_Relation. However, in this case, node.parents returns a list of edges, which results the ask for the node's parents into something of the sort:

Node.where(id: Edge.where(child: node))


The question is; is this a code smell? Can we do this relationship better where node.parents would return Node::ActiveRecord? If so, how would one access edge.edge_properties?
You should take a look at the through: option of has_many:

class Node
  has_many :parent_edges, class_name: "Edge", foreign_key: :child_id
  has_many :parents, through: :parent_edges

  # ditto for children
end


Getting at edge_properties depends on the version of Rails you are using.