Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails Neo4j查找与某个节点相关的所有节点_Ruby On Rails_Neo4j_Neo4jrb - Fatal编程技术网

Ruby on rails Rails Neo4j查找与某个节点相关的所有节点

Ruby on rails Rails Neo4j查找与某个节点相关的所有节点,ruby-on-rails,neo4j,neo4jrb,Ruby On Rails,Neo4j,Neo4jrb,假设有两种型号: class A include Neo4j::ActiveNode property :name, type: String has_many :in, :bs, type: :HAS_B end class B include Neo4j::ActiveNode property :name, type: String end 以及以下节点和关系: a1 <- b1 a2 <- b1 a3 <- b2 a1 <- b

假设有两种型号:

class A
  include Neo4j::ActiveNode
    property :name, type: String
    has_many :in, :bs, type: :HAS_B
end

class B
  include Neo4j::ActiveNode
    property :name, type: String
end
以及以下节点和关系:

a1 <- b1
a2 <- b1
a3 <- b2
a1 <- b2
a1我就是这样做的:

A.as(:a).B.where(name: [b1])
这里,对于多个
b
,只需发送所有所需
b
的数组即可


请注意,此查询提供了所有
a
,它们连接到数组中的
b
中的任何一个,这符合我在本例中的要求。如果您想要提供连接到所有
a
的所有
a
,这将不起作用。但是,如果有人遇到这样的查询,请将其发布在评论中,我会将其包含在本文中。

您应该将
:bs
关联添加到
B类

class B
  include Neo4j::ActiveNode
    property :name, type: String
    has_many :out, :bs, type: :HAS_B, model_class: :A
end
然后,在找到一个特定的节点B之后,您只需执行
B.bs
。例如
B.where(id:some_id).bs

如果要查找标签A中与节点b1或b2相关的所有节点,可以执行以下操作

A.all.branch { bs.as(:b).where("b.uuid IN [$b1_id, $b2_id]") }.params(b1_id: b1_id, b2_id: b2_id)
A.all.branch { bs.where(id: b1_id) }.branch { bs.where(id: b2_id) }
如果要查找标签A中与节点b1和b2相关的所有节点,可以执行以下操作

A.all.branch { bs.as(:b).where("b.uuid IN [$b1_id, $b2_id]") }.params(b1_id: b1_id, b2_id: b2_id)
A.all.branch { bs.where(id: b1_id) }.branch { bs.where(id: b2_id) }

为什么会有人不加评论就投反对票。如果需要…如果不清楚或其他什么,建议改进。