简体   繁体   English

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

[英]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:所以我的用户 model 是:

    has_many :deposits

account model belongs_to:user deposit model belongs_to:user账户 model 属于_to belongs_to:user存款 model 属于_to 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在我的 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 somehow am getting this error 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>'

What Do I Do.....???????????????????我该怎么办.....???????????????????

You probably have less @deposits than @accounts so d is nil, thats the behaviour of zip https://apidock.com/ruby/Array/zip你的@deposits可能比@accounts少,所以d为零,这就是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.如果我认为正确的话,您需要在数据库中建立存款和账户之间的链接,然后获取每个账户并检查其所有相关存款。

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

相关问题 如何在ROR中设置窗口的名称? - How do I set the name of a window in ROR? 如何将这两个表与两个模型合并? - How do I merge these two tables with two Models? 如何在我的RoR应用程序中使用此jquery代码? - How do I make this jquery code work in my RoR app? 在RoR中,我如何动态显示和限制允许的字符数? - In RoR how do I dynamically show and limit the number of characters allowable? 如何在一个时间间隔内循环这两个不透明度函数? - How do I loop these two opacity functions in an interval? 如何在 v-for 循环中匹配两个 arrays 的并集 - How do I match the union of two arrays in a v-for loop 如何完成此循环? 连接前两个字符 - How do I finish this loop? Concatenate two previous characters 我如何在这个 for 循环中包含“两个字符”,无论是两个数字、两个字母还是它们的组合? - How do I include "two characters", whether its two numbers, or two letters, or a combination of them in this for loop? JsPDF Autotable:如何并排显示跨越多个页面的两个表格? - JsPDF Autotable: How do I display two tables side by side that span multiple pages? 我如何编写 SQL 查询以从两个表中获取以下输出(使用 Nodejs) - how do i write SQL Query for getting following output from two tables (With Nodejs)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM