[英]Neo4j gem - Handling collection query with index
To avoid making the last question full of edits, I am spinning off a new question on debugging this. 为了避免使最后一个问题充满编辑内容,我在调试此问题时提出了一个新问题。 Original question was this Neo4j gem - Plucking multiple nodes/relationships 最初的问题是这个Neo4j宝石-选择多个节点/关系
This is sorta where I ended up. 这是我最终的结果。 There is some flaw with the day detection but as a query it does work for now. 日间检测存在一些缺陷,但作为查询,它确实可以使用。 @collection returns a slew of things as written. @collection返回一系列书面内容。
Event.rb Event.rb
def self.reminder
one_day = 1.day.to_i
time = Time.zone.now.to_i
@collection = Event.as(:e).where( "( ( e.date_start - {current_time} ) / {one_day_p} ) < {one_p} " ).users(:u, :rel).where(setting_reminder: true).rel_where(reminded: false ).params(one_day_p: one_day, current_time: time, one_p: 1).pluck(:e, 'COLLECT(u)', 'COLLECT(rel)')
@collection.each do |event, users, rels|
users.each_with_index do |user, i|
UserMailer.reminder(event,user[i]).deliver
end
rels.each_with_index do |rels, i|
rels[i].reminded = true
end
end
end
I originally had it as the last answer did, but I think I need to track both the indexes of the user and the rel nested within the each block. 我最初是用它作为最后一个答案的,但是我想我需要跟踪用户的索引和嵌套在每个块中的rel。
Running in rails c, when I run Event.reminder
I get 在rails c中运行,当我运行Event.reminder
我得到了
TypeError: 0 is not a symbol
What's wrong with my nested loop? 我的嵌套循环有什么问题?
@collection
is one big array that contains other arrays. @collection
是一个包含其他数组的大数组。 You can't do @collection.each do |event, users, rels|
您不能执行@collection.each do |event, users, rels|
, you need to return each array within it and then loop through those. ,您需要返回其中的每个数组,然后遍历这些数组。 Two ways: 两种方式:
@collection = Event.as(:e).where( "( ( e.date_start - {current_time} ) / {one_day_p} ) < {one_p} " ).users(:u, :rel).where(setting_reminder: true).rel_where(reminded: false ).params(one_day_p: one_day, current_time: time, one_p: 1).pluck(:e, 'COLLECT(u)', 'COLLECT(rel)')
@collection.each do |row|
event = row[0]
users = row[1]
rels = row[2]
users.each_with_index do |user, i|
UserMailer.reminder(event, user).deliver
rels[i].reminded = true
rels[i].save
end
end
# OR
events = collection.map { |row| row[0] }
users = collection.map { |row| row[1] }
rels = collection.map { |row| row[2] }
events.each_with_index do |event, i|
UserMailer.reminder(event, users[i]).deliver
rels[i].reminded = true
rels[i].save
end
I think you just need to do a normal each: 我认为您只需要做一个普通的:
users.each do |user|
UserMailer.reminder(event,user).deliver
end
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.