简体   繁体   English

Neo4j gem-查询特定关系

[英]Neo4j gem - Query for specific relationships

There are numerous ways to get a patterns of nodes and relationships and even look up relationships themselves based on relationship properties (eg EnrolledIn.where(since: 2002) but what's the best way to do obtain the relationship based on the specific properties of the nodes. for example 有很多方法可以获取节点和关系的模式,甚至可以根据关系属性查找关系本身(例如, EnrolledIn.where(since: 2002)但是根据节点的特定属性获取关系的最佳方法是什么? 。 例如

current_user -[:relationship]->event 当前用户-[:关系]->事件

I know the event ID and so to me, the most logical way is to query the pattern with the event.id as one of the properties being searched. 我知道事件ID,因此对我而言,最合乎逻辑的方法是使用event.id作为要搜索的属性之一来查询模式。 I have something like this right now 我现在有这样的事情

event_rel = current_user.events(:e, :rel).where(id: event_id).pluck(:rel)

The above does not work and it returns an array 上面的方法不起作用,它返回一个数组

What you're doing is fine, pluck(:rel) will always return an array. 您正在执行的操作很好, pluck(:rel)将始终返回一个数组。 If you know you only want one rel, just do 如果您知道自己只想要一个rel,那就做吧

event_rel = current_user.events(:e, :rel).where(id: event_id).limit(1).pluck(:rel).first

If you're using the master branch from github, you can use first_rel_to method, which (along with match_to ) is changing the way I'm using the gem lately. 如果您使用的是来自github的master分支,则可以使用first_rel_to方法,该方法(以及match_to )正在改变我最近使用gem的方式。

event_rel = current_user.events.first_rel_to(event_id)

The ability to give it an ID was added a couple days ago, it won't work if you're pulling from Rubygems. 几天前就添加了为其提供ID的功能,如果您是从Rubygems提取的,则它将不起作用。 The version of the method in 3.0.4 doesn't accept an ID, it only accepts a full node. 3.0.4中的方法版本不接受ID,仅接受完整的节点。 If you happen to have the node loaded, you can do event_rel = current_user.events.first_rel_to(event) in the released version. 如果碰巧已加载了节点,则可以在发行版中执行event_rel = current_user.events.first_rel_to(event)

If you don't want to use pluck, you can do event_rel = current_user.events.where(id: event_id). limit(1).each_rel.first 如果您不想使用event_rel = current_user.events.where(id: event_id). limit(1).each_rel.first ,则可以执行event_rel = current_user.events.where(id: event_id). limit(1).each_rel.first event_rel = current_user.events.where(id: event_id). limit(1).each_rel.first . event_rel = current_user.events.where(id: event_id). limit(1).each_rel.first Just be aware that that's going to return every rel between those two nodes to Ruby if you omit limit(1) , so don't use it unless you know you're only going to get one back. 请注意,如果您省略limit(1) ,它将把这两个节点之间的每个rel返回给Ruby,因此除非您知道只取回一个,否则不要使用它。 I usually add limit(1) when I'm doing this, even if I only think I'm going to get one rel back, cause I like to be safe. 我通常会在执行此操作时添加limit(1) ,即使我只是认为自己会再获得一次回报,因为我喜欢安全。

I suggest you point your Gemfile at the most recent commit in Github, just read through the new section in the wiki about the changes in 4.0. 我建议您将Gemfile指向Github中的最新提交,只需通读Wiki中有关4.0更改的新部分即可。 It's stable and the new features are so cool. 它很稳定,新功能非常酷。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM