[英]How can I get all ActiveRel between two ActiveNode in Neo4j ruby?
Suppose we have 假设我们有
User
User
name
: String name
:String Transaction
Transaction
amount
: Float amount
:浮动 representing a simplized Bitcoin-like transaction, where a User sends coins to other Users. 代表简化的比特币式交易,用户将硬币发送给其他用户。 A Transaction has a property
amount
which shows how much coin you're sending to the to_node
from from_node
. 交易有一个属性
amount
,显示您从from_node
发送到to_node
硬币from_node
。
Then now I want to get all the transactions (either uni- or bi- directionally) between Alice and Bob. 那么现在我想得到Alice和Bob之间的所有交易(无论是单向还是双向)。 How can I do this?
我怎样才能做到这一点?
# 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)
I was surprised that the latter isn't acceptable. 我很惊讶后者是不可接受的。 It includes
bob
directly into CYPHER. 它包括直接进入CYPHER的
bob
。
Using Neo4jrb v8.0.0 使用Neo4jrb v8.0.0
I struggled and came to know I can do something like this: 我挣扎着,知道我可以这样做:
alice.receivers.match_to(bob).pluck(:rel1)
With this we can get all the transactions sent from alice
to bob
, but I think it's not a good idea using the magical :rel1
here (which is available because it is automatically written in the CYPHER, and match_to
returns a QueryProxy
object: match_to
) 有了这个,我们可以获得从
alice
发送到bob
所有事务,但我认为使用magical不是一个好主意:rel1
here(可用,因为它自动写在CYPHER中, match_to
返回QueryProxy
对象: match_to
)
But it is always not much encouraged to get just the Rels. 但是,只获得Rels总是没有太大的鼓励。
Relatively, we could 相对而言,我们可以
alice.receivers.match_to(bob).rel_where(amount: 1.0) # Txs which amount are 1.0btc
and 和
alice.receivers.match_to(bob).each_rel.sum {|rel| rel.amount } # All the sent money!
Note that you can't do this (somehow): 请注意,你不能这样做(不知何故):
alice.receivers.match_to(bob).rel_where("rel1.amount < 10.0")
but can go-around with this: 但可以解决这个问题:
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`
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.