0

考虑这个简单的设置:

class Person
  include Neo4j::ActiveNode

  property :name, type: String

  has_many :out, :follows, model_class: Person, rel_class: Friendship
  has_many :in, :followed_by, model_class: Person, rel_class: Friendship
end

class Friendship
  include Neo4j::ActiveRel

  property :key, type: String

  type 'friendship'
  from_class Person
  to_class Person
end

我将如何在所有Friendships 中搜索匹配条件的那些?(例如Friendship某个键的 s)。

在一封电子邮件中,Brian Underwood 向我指出了这个片段:

ModelClass.association_name(:node_var, :rel_var).where("rel_var = 'some_condition'")

我试过玩弄它,但不明白。是ModelClass一个ActiveNode还是ActiveRel实例?什么是:node_var:rel_var

4

1 回答 1

2

如果您想搜索具有特定key属性的每个友谊,您可以这样做:

Person.all.follows.rel_where(key: your_key_var)
# OR
Person.all.follows(:f, :r).where('r.key = {key}').params(key: your_key_var)

这些都将MATCH (p:Person)-[r:friends]->(f:Person)或多或少地生成 ,第一个示例使用自动定义的节点标识符,第二个示例f用于目标 Friend 节点和r关系,由:f, :r参数给出。之后, ato_a将在链的 END 返回朋友,或者您可以调用pluck其中一个:f:r返回给定的对象。

model_class选项始终描述关联另一端的 NODE 类。在 Brian 的示例中,node_varrel_var是 Cypher 将在它创建的语句中使用的标识符的通用名称。

于 2015-04-13T23:56:38.730 回答