简体   繁体   English

Neo4j gem - 采集多个节点/关系

[英]Neo4j gem - Plucking multiple nodes/relationships

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)

The goal is to obtain the events where the start_date is less than a day away. 目标是获取start_date少于一天的events Hypothetically I've tried to pluck the event, the user and the relationship. 假设我试图采摘事件,用户和关系。 I need to do a different action with each. 我需要对每个人采取不同的行动。

I need to get all the users for each event and perform an email action for each user. 我需要为每个event获取所有users并为每个用户执行电子邮件操作。 In that action, the email needs to have access to that event . 在该操作中,电子邮件需要有权访问该event

Next, I need to update the rel.reminded property to true . 接下来,我需要将rel.reminded属性更新为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. 我开始单独做这个,但我必须进行额外的查询才能实现。 eg 例如

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)

then I have to 然后我必须

events.each do |event|
# do stuff
end

# do another query that is associated and do more stuff

Update: 更新:

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)')

but rel is not defined with this query. 但是此查询未定义rel

I also wanted to chime in on a couple of points. 我还想提出几点意见。 You should be able to use ActiveSupport to make part of the query easier like this: 您应该能够使用ActiveSupport使查询的一部分更容易,如下所示:

Event.as(:e).where("date_start < {date_threshold}").params(1.day.from_now)

Also, to make it less cluttered I don't see any problem in putting .where("rel.reminded = false") (or using rel_where(reminded: false) as Chris suggested). 另外,为了减少混乱,我没有看到放置.where("rel.reminded = false") (或使用rel_where(reminded: false)克里斯建议的任何问题)。 This isn't data passed in from the user and it doesn't change at different times when you call the query, so it should be just as efficient. 这不是从用户传入的数据,并且在您调用查询时不会在不同时间更改,因此它应该同样有效。

You also may want to use the new query chaining in the v4 of the gem to define a unreminded_users method like this: 您还可能希望使用gem的v4中的新查询链接来定义这样的unreminded_users方法:

class Event
  def self.unreminded_users
    all.rel_where(reminded: false)
  end
end

I've actually not tried doing a rel_where like that in a query chain, but I suspect it will work. 我实际上没有在查询链中尝试过像这样的rel_where ,但我怀疑它会起作用。 Then you'll just have this: 然后你就会有这个:

Event.as(:e).where("e.start_date < {date_threshold}").params(date_threshold: 1.day.from_now)
  .users(:u, :rel).rel_where(reminded: false)
  .pluck(:e,:u, :rel)

First off, if you're using v4 of the gem, use rel_where instead of where with a string for your relationship! 首先,如果你使用的宝石V4,使用rel_where而不是where与你们的关系是一个字符串!

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. 你应该能够改变你的采摘方式pluck(:e, 'COLLECT(u)', 'COLLECT(rel)')它会给你一大堆可用的事件,用户和相关信息,所有事件,用户和服务器都在父数组中分组基于事件。 It'd be organized like this: 它的组织方式如下:

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

The position of each rel matches its user, so rel1a is the relationship between the event and user1a . 每个rel的位置与其用户匹配,因此rel1a是事件和user1a之间的关系。

You can set that to @collection = my_big_query and then do @collection.each do |event, users, rels| 您可以将其设置为@collection = my_big_query ,然后执行@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 . each_with_index用户执行each_with_index ,并且用户的索引将对应于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 %>

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

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