views:

58

answers:

0
+1  Q: 

Graph Edges Rails

I found this recently when trying to do bidirectional relationships in rails (http://www.dweebd.com/sql/modeling-bidirectional-graph-edges-in-rails/)

class Befriending < ActiveRecord::Base
  belongs_to :initiator, :class_name => :User
  belongs_to :recipient, :class_name => :User
  after_create do |b|
    BefriendingEdge.create!(:user => b.initiator, :befriending => b)
    BefriendingEdge.create!(:user => b.recipient, :befriending => b)
  end
end

class BefriendingEdge < ActiveRecord::Base
  belongs_to :user
  belongs_to :befriending
end

class User < ActiveRecord::Base
  has_many :befriending_edges
  has_many :friends, :through => :befriending_edges, :source => :user
  has_many :befriendings, :through => :befriending_edges, :source => :befriending
end

But I just don't quite understand how it works. Can anyone helps explain to me. It looks like a double belongs_to. Just not quite understanding this.

Thanks