Rails 4 seeds.rb 正在更新关联模型

Rails 4 seeds.rb updating associated model

我在 seeds.rb 文件中生成了大量嵌套的 object,运行 遇到了问题。除了绑定到 parent object 的属性外,所有 object 都已正确创建。在以下文件中:

seeds.rb

accounts.each do |i|
    80.times do |j|
        type = types.sample
            case (type)
                ...
            end
        t = AcctTransaction.new
            t.account_id = i.id
            t.transaction_type_id = type
            t.description = description
            t.amount = amount
            # keep transaction in chronological order unless it's the first one
            unless AcctTransaction.exists?(account_id: t.account_id)
                t.date = rand(i.date_opened..Time.now)
            else
                t.date = rand(AcctTransaction.where(account_id: t.account_id).last.date..Time.now)
            end
            t.adjusted_bal = i.balance + t.amount
        i.update_attribute :balance, t.adjusted_bal
        t.save
        account_transactions << t
    end
end

Rake db:seed 运行s 没有错误,并为每个帐户生成 80 笔交易(约 12000 笔交易)。 唯一的问题是adjusted_bal(AcctTransactions 模型)生成的值和 的更新值balance(账户模型)不正确。他们只需要反映当前余额+交易金额的计算。

是我的计算或循环本身有问题,还是我使用了错误的方法将这些计算值分配给它们各自的模型?我已经尝试了 100 种不同的方法,但没有成功。请帮忙。

它是 Rails 4.1.8 / Ruby 2.1.5。谢谢。

编辑

To clarify..

This part of the seed file should do the following:

  1. For each account (already assigned to "accounts variable" and iterated with "i"), generate 80 unique transactions
  2. Add the amount of the transaction to the account's present balance
  3. Store that value as "adjusted_bal" in the AcctTransactions table, and also use this value to update the "balance" in the Accounts table
  4. The loop runs 80 times and each time, "balance" should be different - having been modified by the previous iteration.

Example

1st iteration:

  • original account balance is 100 (for example).
  • transaction amount (random) is -50.
  • (-50) + 100 = 50. This is stored in the transaction as adjusted_bal
  • Also, the account model's balance column is changed to "50", as this is the account's new balance.

2nd iteration:

  • Now the account balance is 50 (having been updated by the last transaction loop iteration)
  • transaction amount (random) is 25.
  • 25 + 50 = 75. This is stored in the transaction as adjusted_bal
  • Also, the account model's balance column is changed to "75" also, as this is the account's new balance.

(Do this 80 times)

Hope that makes it more clear. Thanks

编辑 (不正确的)结果的屏幕截图...

正如您从该屏幕剪辑(放大)中看到的那样,标题中的账户余额 为 1073.62 美元。在每笔交易中,balance 列(即来自 acct_transaction 模型的 adjusted_bal)正是 金额 从 $1073.62 中减去 - 这意味着账户余额永远不会改变。

我不确定你的代码有什么问题,我觉得没问题,但请允许我为你稍微清理一下,它也应该运行得更快,因为它执行的查询更少,它可能会帮助你弄清楚问题出在哪里..也许哈哈

accounts.each do |account|
  80.times do    # no need for iterator
    type = types.sample
    case (type)
      # ...
    end

    # this will save and insert the new transaction in the collection
    # at the same time
    # will save you the second query that updates the relation
    account_transactions.create do |transaction|
      transaction.account_id = account.id
      transaction.transaction_type_id = type
      transaction.description = description
      transaction.amount = amount
      transaction.adjusted_bal = account.balance + transaction.amount

      # doing a first_or_initialize, will save you one query
      # in loops where the record actually exist
      transaction.date =
        (t = AcctTransaction.where(account_id: t.account_id).first_or_initalize).persisted? ?
        rand(account.date_opened..Time.now) :
        rand(t.date..Time.now)
    end 
  end
  # save only once after the 80 loops, before moving to the next account
  # could save you up to 80 queries in each loop 
  account.save
end