2009-05-07 1 views
2

レール で双方向の関係をやろうとしたとき、私はグラフは、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 

http://www.dweebd.com/sql/modeling-bidirectional-graph-edges-in-rails/)最近、これを見つけた。しかし、私はちょうどかなりそれがどのように動作するか理解していないが、エッジ。誰も私に説明するのを助けることができますか?それは二重のbelongs_toのように見えます。これをあまりよく理解していない。

おかげ

答えて

0
  1. 私は
  2. 私の友人はまた、グラフ(HTTPを使用してこれをモデル化する方法ユーザー

ある友人持つユーザー

  • だ:// ENを.wikipedia.org /ウィキ/ Graph_%28mathematics%29)は、その

    • ノードであります
    • ユーザー/友人を表す友情が

    を結ぶ表すエッジはそうです:データベースの用語では、「ユーザーは、ユーザーに属している」:私の友人もユーザーです。しかし、さらに、友情は双方向です。私たちが友人なら、私はあなたの友人ですあなたは私の友達です。

    また、エッジ/リレーションシップを保存するために別のモデルを使用すると、友だちに関する追加情報(「友だち」など)を保存する可能性があります。