繁体   English   中英

我如何在 rake 任务 ROR 中循环两个表

[英]How Do i loop two tables in a rake task ROR

所以我正在编写一个系统,每个用户都有一个帐户和存款。 所以我的用户 model 是:

    has_many :deposits

账户 model 属于_to belongs_to:user存款 model 属于_to belongs_to:user

在我的 rake 任务中,我试图查找并验证每笔存款,一旦验证,equivqlent 就会更新为 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不知何故收到此错误

/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
daveskeen24@gmail.com
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>'

我该怎么办.....???????????????????

你的@deposits可能比@accounts少,所以d为零,这就是zip https://apidock.com/ruby/Array/zip的行为

如果我认为正确的话,您需要在数据库中建立存款和账户之间的链接,然后获取每个账户并检查其所有相关存款。

暂无
暂无

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

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