【问题标题】:Using has_many "both" in neo4j.rb在 neo4j.rb 中使用 has_many “both”
【发布时间】:2015-04-08 15:01:34
【问题描述】:

我正在寻找一种在 Users 之间建立关系的方法,您可以在 Neo4j.rb 中同时使用 inoutboth

这是我目前所拥有的:

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]

【问题讨论】:

    标签: ruby neo4j neo4j.rb


    【解决方案1】:

    这是预期的行为。 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 问题

    【讨论】:

    • 啊,这很有道理。深入研究一下,我认为无论哪种方式我都不需要这个功能。
    • 太棒了 ;) 我忘了提的另一件事:你应该养成像这样为model_class 使用字符串的习惯:model_class: 'User'
    • 会的!非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2014-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多