2012-07-17 37 views
1

我使用的Neo4j 2.0.1與3.2.1的軌道。我收到一個似乎很基本的錯誤,但我似乎無法解決它。我會很感激任何幫助!未定義_other_node未繳:NilClass錯誤的Neo4j與軌道

這裏是我的代碼片段:

rels1 = identity1.rels(:outgoing,:friends) 
if !rels1.nil? and rels1.count > 0 
    friendships12 = rels1.to_other(identity2) 
end 

其中對identity1和identity2是Neo4j的:: Rails的子類::模型對象。

我得到的錯誤是上線「friendships12 = ....」和它說

"undefined method `_other_node' for nil:NilClass" 

我在做什麼錯?起初,我嘗試了很明顯的:

friendships12 = identity1.rels(:outgoing,:friends).to_other(identity2) 

這是基於軌道指南Neo​​4j的(http://neo4j.rubyforge.org/guides/basic.html,請參閱「兩個節點之間的關係尋找」)。但是,這給了我同樣的錯誤,這就是爲什麼我如上所述嘗試。但錯誤仍然存​​在。

回答

0

你嘗試做 '使rels1'?

如果有身份和關係[我想有]之間的一個一對多的關係,做identity1.rels會返回一個數組。 您可以檢查,如果這是做發生「使rels1」,如果rels1是一個數組,那麼你需要做的rels1.first得到的關係對象,然後在其上to_other做。

編輯:我通過http://neo4j.rubyforge.org/guides/basic.html去了,注意到這一點:

node1.rels # => an *Enumerable* of all incoming and outgoing relationship of any type 

這意味着,你需要做的是這樣

identity1.rels(:outgoing,:friends).to_other(identity2) do |x| 
# Your code that works with relation object x, here 
end 

從本質上講,你通過每一個關係的迭代對象。

閱讀[http://ruby.bastardsbook.com/chapters/enumerables/]以獲取更多信息。

2

我得到了同樣的錯誤,我不知道原因。我通過使用select和尋找末端節點來解決它。

identity1.rels(:outgoing, :friends).select{|r| r.end_node == identity2}.first 

此外,請確保在創建關係後保存節點。

相關問題