难题:ActivereCord对象持续存在,但无法检索到查找
我使用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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您应该使用 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
我最终通过将分类账显式传递到创建下载工厂的帐户工厂来解决此问题,如下所示:
突然,消失的分类账减少了。我相信 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:
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.