难题:ActivereCord对象持续存在,但无法检索到查找

发布于 2025-01-19 02:48:26 字数 936 浏览 5 评论 0原文

我使用AR在非轨道应用程序中遇到了一个头抓手,但我无法弄清楚。我将大量简化,但这是本质:我有一个属于分类帐的下载对象。在我的单元测试之一中,我正在遇到这一点:

dl = create(:download, account: checking)
dl.ledger
 => <Byr::Ledger:0x00007fd176ce4740

 id: 2,
 name: "test_ledger_1",
 org_name: "Test Ledger, Inc.",
 street1: nil,
 street2: nil,
 city: nil,
 state: nil,
 zip: nil,
 created_at: 2022-04-03 13:13:53.734003153 UTC,
 updated_at: 2022-04-03 13:13:53.792451592 UTC,
 default_account_id: 21,
 short_name: "Test_ledger_1",
 parent_percent: nil,
 parent_id: nil,
 accessed_at: 2022-04-03 13:13:53.791911547 UTC,
 start_date: Mon, 01 Jan 2018,
 cost_method: "fifo",
 end_date: nil
> dl.ledger.peristed? => true
> Ledger.find(2) => nil with eval error: Couldn't find Byr::Ledger with 'id'=2

我使用factory_bot创建下载DL,进而为它构建了一个分类帐,并声称可以坚持使用ID = 2。但是,当我尝试使用Ledger.find.find(2)找到Ledger时,它不在Postgresql db中。

有人知道这里会发生什么吗?

I am experiencing a head-scratcher in a non-Rails app using AR that I cannot figure out. I will simplify greatly, but here is the essence: I have a Download object that belongs to a Ledger. In one of my unit tests, I am experiencing this:

dl = create(:download, account: checking)
dl.ledger
 => <Byr::Ledger:0x00007fd176ce4740

 id: 2,
 name: "test_ledger_1",
 org_name: "Test Ledger, Inc.",
 street1: nil,
 street2: nil,
 city: nil,
 state: nil,
 zip: nil,
 created_at: 2022-04-03 13:13:53.734003153 UTC,
 updated_at: 2022-04-03 13:13:53.792451592 UTC,
 default_account_id: 21,
 short_name: "Test_ledger_1",
 parent_percent: nil,
 parent_id: nil,
 accessed_at: 2022-04-03 13:13:53.791911547 UTC,
 start_date: Mon, 01 Jan 2018,
 cost_method: "fifo",
 end_date: nil
> dl.ledger.peristed? => true
> Ledger.find(2) => nil with eval error: Couldn't find Byr::Ledger with 'id'=2

I use factory_bot to create the Download, dl, which in turn builds a ledger for it to go with, which purports to be persisted with id=2. But when I try to find the ledger with Ledger.find(2), it's not in the postgresql db.

Anybody have any idea what might be going on here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

和影子一齐双人舞 2025-01-26 02:48:26

我认为您应该使用 faker + factory_bot 和模拟资源以访问所需的参数。
在这里在这里 。希望这会有所帮助

I think you should use something like faker + factory_bot and mock resources to get access to params you need.
Here an example of use. Hope this helps

嗼ふ静 2025-01-26 02:48:26

我最终通过将分类账显式传递到创建下载工厂的帐户工厂来解决此问题,如下所示:

let(:ldg) { create(:ledger, name: 'Willy') }
let(:checking) { create(:bank_account, ledger: ldg) }
let(:dl) { create(:download, account: checking) }

突然,消失的分类账减少了。我相信 cuplrit 可能是 DatabaseCleaner 将自动生成的账本从一个示例擦除到下一个示例。仅针对所讨论的示例使用显式分类帐可以防止它在我脚下被删除。

谢谢@Joe,您对此进行了一些思考。

I finally got this to pass by explicitly passing the ledger into the Account factory on which the Download factory was created, like this:

let(:ldg) { create(:ledger, name: 'Willy') }
let(:checking) { create(:bank_account, ledger: ldg) }
let(:dl) { create(:download, account: checking) }

Suddenly, the disappearing ledgers abated. I believe the cuplrit may have been DatabaseCleaner wiping away the automatically-generated ledger from one example to the next. Using an explicit ledger just for the example in question kept it from being deleted underneath my feet.

Thanks, @Joe, for giving this some thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文