Neo4j gem-选择多个节点/关系 [英] Neo4j gem - Plucking multiple nodes/relationships

查看:366
本文介绍了Neo4j gem-选择多个节点/关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能暂时无法实现,但是我有这样的查询

This may not be possible as it is right now but I have a query like so

events = Event.as(:e).where("(( e.date_start - {current_time} )/{one_day_p}) < 1 ").users(:u, :rel).where("rel.reminded = {reminded_p}" ).params(reminded_p: false, one_day_p: one_day, current_time: time).pluck(:e,:u, :rel)

目标是获取开始日期不到一天的events.假设我尝试提取事件,用户和关系.我需要对每个对象执行不同的操作.

The goal is to obtain the events where the start_date is less than a day away. Hypothetically I've tried to pluck the event, the user and the relationship. I need to do a different action with each.

我需要获取每个event的所有users,并为每个用户执行电子邮件操作.在该操作中,电子邮件需要有权访问该event.

I need to get all the users for each event and perform an email action for each user. In that action, the email needs to have access to that event.

接下来,我需要将rel.reminded属性更新为true.

Next, I need to update the rel.reminded property to true.

是否可以同时提取所有这些信息以组织和执行这些任务?我开始单独进行此操作,但是我必须进行额外的查询才能实现.例如

Is there a way to pluck all these simultaneously to be able to organize and perform these tasks? I started doing this individually but I have to make extra queries to make it happen. e.g.

events = Event.as(:e).where("(( e.date_start - {current_time} )/{one_day_p}) < 1 ").users(:u, :rel).where("rel.reminded = {reminded_p}" ).params(reminded_p: false, one_day_p: one_day, current_time: time).pluck(:e)

那我必须

events.each do |event|
# do stuff
end

# do another query that is associated and do more stuff

更新:

结合答案,我有类似这样的东西,但还没有清理时间的方法.我添加了一个搜索用户的位置,因为稍后在电子邮件功能中需要考虑这一点.因此,我不确定将其包含在查询中是否比在每个用户外部进行查询更有效.

Incorporating the answers, I have something like this without the cleaned up time method yet. I added in a where search for user as I will need to factor that in later in the email function. So I am not sure if it's more efficient to include it in the query vs doing it outside per user.

@collection = Event.as(:e).where("(( e.date_start - {current_time} )/{one_day_p}) < 1 ").users(:u).where(setting_reminder: true).rel_where(reminded: false ).params(one_day_p: one_day, current_time: time).pluck(:e, 'COLLECT(u)', 'COLLECT(rel)')

但此查询未定义rel.

推荐答案

首先,如果您使用的是v4版本的gem,请使用rel_where而不是where并带有一个用于您关系的字符串!

First off, if you're using v4 of the gem, use rel_where instead of where with a string for your relationship!

您应该可以将获取率更改为pluck(:e, 'COLLECT(u)', 'COLLECT(rel)'),它将为您提供大量的事件,用户和rel,它们都基于事件归为父数组.它会这样组织:

You should be able to change your pluck to pluck(:e, 'COLLECT(u)', 'COLLECT(rel)') and it'll give you a big enumerable of events, users, and rels, all grouped in parent arrays based on event. It'd be organized like this:

  [
    [event1, [user1a, user1b, user1c], [rel1a, rel1b, rel1c]],
    [event2, [user2a, user2b, user2c], [rel2a, rel2b, rel2c]]
  ]

每个rel的位置都与其用户匹配,因此rel1a是事件和user1a之间的关系.

The position of each rel matches its user, so rel1a is the relationship between the event and user1a.

您可以将其设置为@collection = my_big_query,然后在视图中执行@collection.each do |event, users, rels|以循环浏览.您将对用户执行each_with_index,并且用户索引将与rels中的位置相对应.看起来像这样:

You can set that to @collection = my_big_query and then do @collection.each do |event, users, rels| in your view to loop through. You'd do each_with_index on users and the index of the user would correspond to the position within rels. It'd look something like:

<%= @collection.each do |event, users, rels| %>
  <%= event.name %>
  <% users.each_with_index do |user, i| %>
    <%= user.name %> said they were attending at <%= rels[i].confirmed_at %>.<br />
  <% end %>
<% end %>

这篇关于Neo4j gem-选择多个节点/关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆