[英]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.