【问题标题】:Neo4j.rb query in and out separately for both relationshipNeo4j.rb 对两种关系分别查询进出
【发布时间】:2015-01-07 15:42:59
【问题描述】:

这是我的 Neo4j 模型

class User
 include Neo4j::ActiveNode
 has_many :both, :followers, type: :following, model_class: 'User'
end

user1 有 2 出和 1 入,如下所示

用户 1 ---> 用户 2

用户 1 ---> 用户 3

用户1

如果我像下面这样查询,它会同时返回输入/输出

user1.followers.count #returns 3

我想分别查询 inoutUser

我怎样才能用 Neo4j.rb 做到这一点..

提前致谢。

【问题讨论】:

    标签: ruby-on-rails ruby neo4j neo4j.rb


    【解决方案1】:

    由于关联规定了匹配 Cypher 的编写方式,因此在调用该方法以更改其行为时,您无法覆盖该方法。解决方案是再创建两个关联。

    class User
      include Neo4j::ActiveNode
      has_many :both, :followers, type: :following, model_class: 'User'
      has_many :out,  :following, type: :following, model_class: 'User'
      has_many :in,   :followed_by, type: :following, model_class: 'User'
      end
    end
    
    # Or, to make it feel more Ruby...
    
    class User
      include Neo4j::ActiveNode
      [[:both, :followers], [:out, :following], [:in, :followed_by]].each do |dir, assoc|
        has_many dir, assoc, type: :following, model_class: 'User'
      end
    end
    
    
    2.2.0 :008 >   User.followers.to_cypher
     => "MATCH (node2:`User`), (result_followers:`User`), node2-[rel1:`following`]-(result_followers:`User`)" 
    2.2.0 :009 > User.following.to_cypher
     => "MATCH (node2:`User`), (result_following:`User`), node2-[rel1:`following`]->(result_following:`User`)" 
    2.2.0 :010 > User.followed_by.to_cypher
     => "MATCH (node2:`User`), (result_followed_by:`User`), node2<-[rel1:`following`]-(result_followed_by:`User`)" 
    

    【讨论】:

    • 我只是在写一个类似的声明,尽管你的解释性要强得多。
    • 当我在发布后的几秒钟内看到支持投票时,我想了很多! :-)
    • @subvertallchris 很好的答案.. +1 使用 to_cypher 方法
    猜你喜欢
    • 1970-01-01
    • 2021-03-21
    • 2016-10-11
    • 2014-10-09
    • 2019-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多