Ruby on rails 如何使用Neo4j.rb导航Neo4j

Ruby on rails 如何使用Neo4j.rb导航Neo4j,ruby-on-rails,neo4j,Ruby On Rails,Neo4j,我正在使用Neo4j 2.0.1和Neo4j.rb3.0 我有以下疑问: <% xxx = Neo4j::Session.query('match (q:Complex_Type)<-[:_IS_A]-(m) return m;') %> 现在,继续,哪种方法是获取标签的正确方法(不使用get&post) 短暂性脑缺血发作 Paolo假设至少Neo4j.rb 3.0 RC2或更高: 您可以将查询修改为如下所示: result = Neo4j::Session.query('

我正在使用Neo4j 2.0.1和Neo4j.rb3.0

我有以下疑问:

<%  xxx = Neo4j::Session.query('match (q:Complex_Type)<-[:_IS_A]-(m) return m;') %>
现在,继续,哪种方法是获取标签的正确方法(不使用get&post)

短暂性脑缺血发作


Paolo

假设至少Neo4j.rb 3.0 RC2或更高:

您可以将查询修改为如下所示:

result = Neo4j::Session.query('match (q:Complex_Type)<-[:_IS_A]-(m) return m, LABELS(m) as labels;')
result.each do |response|
  response.m
  # => the node
  response.labels
  # => an array containing strings of the node's labels
end
…这有点傻

如果您想将其改写为更友好一点,您也可以这样做:

result = Neo4j::Session.query.match('(q:Complex_Type)<-[:_IS_A]-(m)').return('m, LABELS(m) as labels')
从这里开始,您可以轻松地:

a_complex_type.other_things.each { |m| m.labels }
我认为这样做的缺点是,当您调用
labels
时,它可能会执行额外的cypher查询,因此如果这与您有关,您可以这样做:

# to return structs that respond to 'm' or 'labels':
a_complex_type.other_things.query_as(:m).return(:m, 'labels(m) as labels')
# for an array where element 0 is the returned 'm' node, element 1 is an array of labels:
a_complex_type.other_things.query_as(:m).pluck(:m, 'labels(m)')
# note that you can omit 'as labels' here since you aren't dealing with a struct
class ComplexType
  include Neo4j::ActiveNode
  has_many :in, :other_things, model_class: false, type: '_IS_A'
end
a_complex_type.other_things.each { |m| m.labels }
# to return structs that respond to 'm' or 'labels':
a_complex_type.other_things.query_as(:m).return(:m, 'labels(m) as labels')
# for an array where element 0 is the returned 'm' node, element 1 is an array of labels:
a_complex_type.other_things.query_as(:m).pluck(:m, 'labels(m)')
# note that you can omit 'as labels' here since you aren't dealing with a struct