Rails 中的一条记录如何既能持久化又不能同时存在?

问题描述 投票:0回答:1

我试图理解为什么我找不到附加到记录的文件,因此我在代码中的某个点放置了断点

after_save
回调期间(发生这种情况时请务必注意)。以下是在暂停期间拨打一些电话的输出:

0> self.inspect
=> #<BinSet id: 1131, name: "test upload", ...>

0> self.bins_table.download
=> Error: the evaluation of `self.bins_table.download` failed with the exception 'ActiveStorage::FileNotFoundError'

0> self.bins_table.attached?
=> true

0> self.reload
=> Error: the evaluation of `self.reload` failed with the exception 'Couldn't find BinSet with 'id'=1131'

0> self.id
=> 1131

0> self.persisted?
=> true

0> BinSet.find(1131)
=> Error: the evaluation of `BinSet.find(1131)` failed with the exception 'Couldn't find BinSet with 'id'=1131'

0> self.save!
=> true

0> self.id
=> 1131

0> BinSet.find(1131)
=> Error: the evaluation of `BinSet.find(1131)` failed with the exception 'Couldn't find BinSet with 'id'=1131'

回想一下,这是在

after_save
期间发生的,这意味着记录已被保存。它已被分配了一个ID,
self.persisted?
true
,但在数据库中找不到它。此时记录在哪里?

ruby-on-rails ruby
1个回答
0
投票

如果您参考after_save

的文档(链接的文档已弃用,但这仍然适用于当前回调),您将看到以下内容:

请注意,此回调仍然包含在保存周围的事务中。例如,如果您此时调用外部索引器,它将看不到数据库中的更改。

如果您需要关闭数据库事务以进行操作,我会使用

after_commit

© www.soinside.com 2019 - 2024. All rights reserved.