2015-04-08 34 views
2

我正在尋找一種方式來建立User S之間的關係,在那裏你可以在Neo4j.rb同時使用inoutboth所有。使用的has_many「都」在neo4j.rb

這是我到目前爲止有:

class User 
    include Neo4j::ActiveNode 

    has_many :both, :friends, type: :connection, model_class: User 
    has_many :out, :following, type: :connection, model_class: User 
    has_many :in, :followers, type: :connection, model_class: User 
end 

以下工作:

me = User.create 
you = User.create 

me.followers << you 
me.followers.to_a 
#=> [you] 

you.following.to_a 
#=> [me] 

的上述相反的工作也是如此。但是,這似乎並沒有工作:

me.friends << you 
you.following.to_a 
#=> [] 

或者:

me.followers.to_a 
#=> [] 

然而,這並不:

me.following.to_a 
#=> [you] 

回答

2

這是正常現象。 Neo4j不允許你創建沒有方向的關係。因此,both關聯類型僅用於查詢(即查詢時指定了關係,而不是指向/來自節點的方向)。

由於Neo4j關係總是有方向,所以當您創建與both關聯的關係時,它會將它們創建爲out關係。看到文檔本節:

https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3-Declared-Relationships#all-has_manyhas_one-method-calls-begin-with-declaration-of-direction

現在考慮這個問題,我想知道如果可能Neo4j.rb不應該讓你創建使用both關聯關係。你怎麼看?我也會創建一個Github的問題

+0

啊,這很有道理。深入挖掘它,這是一個功能,我不認爲我需要這種方式。 – bswinnerton

+0

太棒了;)另外我忘了提及:你應該養成像'model_class'這樣的字符串使用習慣,''model_class:'User'' –

+0

會做!萬分感謝。 – bswinnerton