如何在Neo4j ruby​​中的两个ActiveNode之间获取所有ActiveRel?

时间:2017-02-13 14:55:59

标签: ruby-on-rails ruby activerecord neo4j neo4j.rb

假设我们有

  • 节点:User
    • name:String
  • Rel:Transaction
    • amount:Float

表示简化的类比特币交易,其中用户将硬币发送给其他用户。交易有一个属性amount,用于显示您从to_nodefrom_node发送的硬币数量。

然后现在我想得到Alice和Bob之间的所有交易(无论是单向还是双向)。我怎么能这样做?

# user.rb
has_many :out, :receivers, rel_class: :Transaction
has_many :in, :senders, rel_class: :Transaction


# Console
alice = User.find_by(name: "Alice")
bob = User.find_by(name: "Bob")

# I want to do something like this:
Transaction.between(from: alice, to: bob)

# or this:
alice.receivers.rel_where(to_node: bob)

我很惊讶后者是不可接受的。它将bob直接包含在CYPHER中。

使用Neo4jrb v8.0.0

1 个答案:

答案 0 :(得分:0)

我挣扎着,知道我可以做这样的事情:

alice.receivers.match_to(bob).pluck(:rel1)

有了这个,我们可以获得从alice发送到bob的所有交易,但我认为在这里使用神奇的:rel1并不是一个好主意(可用因为它是自动写在CYPHER中的,match_to会返回QueryProxy个对象:match_to

但是,只获得Rels总是没有太大的鼓励 相对而言,我们可以

alice.receivers.match_to(bob).rel_where(amount: 1.0)   # Txs which amount are 1.0btc

alice.receivers.match_to(bob).each_rel.sum {|rel| rel.amount }   # All the sent money!


请注意,您无法以某种方式执行此操作:

alice.receivers.match_to(bob).rel_where("rel1.amount < 10.0")

可以解决这个问题:

query = alice.receivers.match_to(bob)          # create just the query
query.where("#{query.rel_var}.amount < 10.0")  # you can get `:rel1` by `rel_var`