Arrays 比较3个阵列

Arrays 比较3个阵列,arrays,ruby-on-rails,loops,Arrays,Ruby On Rails,Loops,我有3个查询的结果,我想进行分析。我已将它们转换为数组,以尽量避免对数据库造成负担 allBudgets = Budget.all.to_a currentBudgets = Budget.select("account_id, amount, year").where("year = '2021'").to_a actuals = GeneralLedger.all.to_a 让我们假设所有预算 [#<account_id: 1, amount:

我有3个查询的结果,我想进行分析。我已将它们转换为数组,以尽量避免对数据库造成负担

allBudgets = Budget.all.to_a
currentBudgets = Budget.select("account_id, amount, year").where("year = '2021'").to_a
actuals = GeneralLedger.all.to_a
让我们假设所有预算

[#<account_id: 1, amount: 50, 2021>,
#<account_id:1, amount: 100, 2020>,
#<account_id:2, amount: 100, 2021>,
#<account_id:2, amount: 150, 2020>,
#<account_id:3, amount: 175, 2020>]

当我尝试使用可枚举迭代器时,我得到了一次又一次重复的最后一个值。

根据我从您的问题中所了解的情况,我认为您需要使用该方法


根据我对你的问题的理解,我认为你需要使用这种方法


我的原始代码正确地进行了迭代。由于在循环中覆盖了我的变量,实际上是一次又一次地获取最后一个值


fwiw Pulk也可以工作。

我的原始代码正确迭代。由于在循环中覆盖了我的变量,实际上是一次又一次地获取最后一个值

fwiw弹拨也起作用

[#<account_id: 1, amount: 50, 2021>,
#<account_id:2, amount: 100, 2021>]
[#<account_id: 1, amount: 47, 2021>,
#<account_id:1, amount: 110, 2020>,
#<account_id:2, amount: 99, 2021>,
#<account_id:2, amount: 155, 2020>,
#<account_id:3, amount: 170, 2020>]
[[1, 2020, 100],[1, 2020, 110],[1,2021,50],[1,2021,47],
[2,2020,150],[2,2020,155],[2,2021,100],[2,2021,99]]
allBudgets = Budget.all.pluck(:account_id, :amount, :year)
currentBudgets = Budget.where(year: year).pluck(:account_id, :amount, :year)