User7777
User7777

Reputation: 299

How to use the "return" value of an inner loop in an outer loop

I want to use the value of the inner each loop in the outer loop to pass it as an argument.

namespace :office do
  desc "send reminder emails"
  task send_reminder: :environment do
    Office.all.each do |office|
      office.issues.where("issues.amount > 0").each do |issue|
        issue.billings.where("billings.date < ?", Message.last.date).order(:date).last # I will get billing_ids here which I want to pass as arguments in the outer loop.
      end
      ReminderWorker.perform_async(billing_ids)
    end
  end
end

How should I do this?

Upvotes: 1

Views: 125

Answers (1)

Fernand
Fernand

Reputation: 1333

Try this:

    Office.all.each do |office|
      billing_ids=[] #initialize array
      office.issues.where("issues.amount > 0").each do |issue|
        billing_ids << issue.billings.where("billings.date < ?", Message.last.date).order(:date).last.id #add id to array
      end
      ReminderWorker.perform_async(billing_ids)
    end

Upvotes: 1

Related Questions