3

我正在寻找一种方法来在 Neo4j.rb 中同时User使用 、 和所有 sin之间out建立关系。both

这是我到目前为止所拥有的:

class User
  include Neo4j::ActiveNode

  has_many :both, :friends, type: :connection, model_class: User
  has_many :out, :following, type: :connection, model_class: User
  has_many :in, :followers, type: :connection, model_class: User
end

以下作品:

me = User.create
you = User.create

me.followers << you
me.followers.to_a
#=> [you]

you.following.to_a
#=> [me]

与上述相反的方法也有效。但这似乎不起作用:

me.friends << you
you.following.to_a
#=> []

或者:

me.followers.to_a
#=> []

但是,这样做:

me.following.to_a
#=> [you]
4

1 回答 1

4

这是预期的行为。Neo4j 不允许您创建没有方向的关系。因此,both关联类型仅用于查询(即在查询时指定关系,但不指定到/从节点的方向)。

由于 Neo4j 关系始终有一个方向,因此当您使用both关联创建关系时,它会将它们创建为out关系。请参阅文档中的此部分:

https://github.com/neo4jrb/neo4j/wiki/Neo4j-v3-Declared-Relationships#all-has_manyhas_one-method-calls-begin-with-declaration-of-direction

现在考虑一下,我想知道 Neo4j.rb 是否不应该让您使用both关联创建关系。你怎么看?我也会创建一个 Github 问题

于 2015-04-08T15:17:05.940 回答