How Do i loop two tables in a rake task ROR

so am writing a system of which each user has an account and deposits. so my user model is:

    has_many :deposits

account model belongs_to :user deposit model belongs_to :user

In my rake task am trying to find and verify each deposit and once verified, the equivqlent is updated to the account.balance

  desc "Depost Verification per plans"
  task testest2: :environment do

    @users = User.all


    @users.each do |u|
      puts u.email

      u.accounts.each do |a|
      puts  a.first_name
      puts  a.balance
      end

      u.deposits.each do |d|
      puts  d.amount
      end


      @accounts = u.accounts.all
      @deposits = u.deposits.all

      @accounts.each do |b|
        puts b.first_name
      end
    print "Commands working here error begins on the next paragrah"
    print "how do i achieve this loop using the above parameters"

    @accounts.zip(@deposits).each do |account, d|
        if d.status?
        acct_bal = account.balance.to_f + d.amount.to_f
        account.update!(balance: acct_bal)
        puts account.user.username
    
        puts account.balance
        print "Deposit Added"
      else
        acct_bal = account.balance.to_f + 0.00
        account.update!(balance: acct_bal)
        puts account.user.username
        print "Deposit Unverifed"
      end
    end
    


    end


    print "command run .........."



  end

Bt somehow am getting this error

/Users/fortune/.gem/ruby/2.7.2/gems/json-1.8.6/lib/json/common.rb:155: warning: Using the last argument as keyword parameters is deprecated
[email protected]
DayTon
0.0
DayTon
Commands working here error begins on the next paragrahrails aborted!
NoMethodError: undefined method `status?' for nil:NilClass
/Users/fortune/sites/current/dapps/dayton/lib/tasks/deposits.rake:77:in `block (4 levels) in <main>'
/Users/fortune/sites/current/dapps/dayton/lib/tasks/deposits.rake:76:in `each'
/Users/fortune/sites/current/dapps/dayton/lib/tasks/deposits.rake:76:in `block (3 levels) in <main>'

What Do I Do.....???????????????????


Solution 1:

You probably have less @deposits than @accounts so d is nil, thats the behaviour of zip https://apidock.com/ruby/Array/zip

If I think correctly what you need is to have a link in the database between deposits and accounts, then get every account and check all its related deposits.