Rails 上的 Cypher 查询和 Ruby
Cypher query and Ruby on Rails
我有一个这样的 Cypher 查询:
start n=node(*) match n-[:has_comments]->(m) return n,m;
哪个 运行没问题。
我怎样才能从 RoR 运行 这个?
使用 Postgres 和 ActiveModel 时,在控制器中我可以使用类似这样的东西。
@query = "SELECT * FROM <table> WHERE <condition>;"
@result = <ClassName>.connection.execute(@query)
然后我以任何我想要的方式处理@result。
您可以使用 Neo4j::Session.current.query
构建一般的 Cypher 查询。不要再使用 START n=node
,该语法在未来的版本中无效,因此您应该改用 MATCH (n) WHERE ID(n) =
。
query = "MATCH (n)-[:has_comments]->(m) WHERE ID(n) = #{id} RETURN n, m"
result = Neo4j::Session.current.query(query).to_a
这将为您提供一个结构数组,您可以分别通过调用 n
和 m
方法访问结果。我根本不建议你这样做。或者,您可以这样做:
result = Neo4j::Session.current.query.match("(n)-[:has_comments]->(m)").where("ID(n) = {id}").params(id: id).return(:n, :m)
您将以相同的方式访问数据:n
和 m
方法。
我也不建议你这样做。你正在使用 Rails 和 ActiveNode,所以你应该有模型并且能够做到 n_node.as(:n).comments(:m).pluck(:n, :m)
.
我有一个这样的 Cypher 查询:
start n=node(*) match n-[:has_comments]->(m) return n,m;
哪个 运行没问题。 我怎样才能从 RoR 运行 这个? 使用 Postgres 和 ActiveModel 时,在控制器中我可以使用类似这样的东西。
@query = "SELECT * FROM <table> WHERE <condition>;"
@result = <ClassName>.connection.execute(@query)
然后我以任何我想要的方式处理@result。
您可以使用 Neo4j::Session.current.query
构建一般的 Cypher 查询。不要再使用 START n=node
,该语法在未来的版本中无效,因此您应该改用 MATCH (n) WHERE ID(n) =
。
query = "MATCH (n)-[:has_comments]->(m) WHERE ID(n) = #{id} RETURN n, m"
result = Neo4j::Session.current.query(query).to_a
这将为您提供一个结构数组,您可以分别通过调用 n
和 m
方法访问结果。我根本不建议你这样做。或者,您可以这样做:
result = Neo4j::Session.current.query.match("(n)-[:has_comments]->(m)").where("ID(n) = {id}").params(id: id).return(:n, :m)
您将以相同的方式访问数据:n
和 m
方法。
我也不建议你这样做。你正在使用 Rails 和 ActiveNode,所以你应该有模型并且能够做到 n_node.as(:n).comments(:m).pluck(:n, :m)
.