Clam
Clam

Reputation: 945

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

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.

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.

Running in rails c, when I run Event.reminder I get

TypeError: 0 is not a symbol

What's wrong with my nested loop?

Upvotes: 1

Views: 49

Answers (2)

subvertallchris
subvertallchris

Reputation: 5482

@collection is one big array that contains other arrays. You can't do @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

Upvotes: 1

Brian Underwood
Brian Underwood

Reputation: 10856

I think you just need to do a normal each:

  users.each do |user|
    UserMailer.reminder(event,user).deliver
  end

Upvotes: 0

Related Questions