2017-02-25 28 views
0

的關係,所以我創建了Neo4j的使用neo4jRb寶石,我如何才能在HAS_ONE協會

class Client 

    has_one :out , :room, model_class: :Room,rel_class: :AssignedTo 

end 

class Room 
    has_many :in , :clients, rel_class: :AssignedTo, model_class: :Client 

end 

class AssignedTo 
    include Neo4j::ActiveRel 
    from_class :Client 
    to_class :Room 
    type 'assigned_to' 
    property :from_date, type: DateTime 
    property :to_date , type: DateTime 
end 

我要接受從一個房間到客戶端 room.clients的Assigned_to關係HAS_ONE和的has_many關係。 each_with_rel正常工作 但我找不到一種方式來訪問關係: client.room.rel 我試過的所有方法client.room.rel,relationship,assigned_to等似乎都不起作用

回答

0

由於client.roomhas_one關係,默認情況下,neo4jrb獲取關聯代理,然後採集第一個(也是唯一)結果,返回room對象。 room ActiveNode對象沒有.each_with_rel方法。

使用最新版本的Neo4jrb gem(不知道你正在使用哪個版本),你可以做client.room(chainable: true).each_with_rel do |node, rel|這應該工作,與room.clients.each_with_rel do |node, rel|一樣。

has_one關聯的chainable: true選項告訴neo4jrb返回關聯代理(這是您總是通過關聯has_many獲得的)。