counter_cache 在 after_create 挂钩中已过时

发布于 2024-08-22 23:46:50 字数 122 浏览 2 评论 0原文

我依赖模型的 after_create 挂钩中的计数器缓存值。然而,我的钩子在计数器缓存更新之前被调用,从而破坏了计算。

有什么方法可以强制计数器缓存“刷新”,以便我始终在 after_create 中看到最新值?

I rely on a counter cache value in an after_create hook of my model. However, my hook is called before the counter cache gets updated, thus breaking a computation.

Is there any way to force a counter cache "flush" so that I always see an up-to-date value in after_create?

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

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

发布评论

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

评论(1

澉约 2024-08-29 23:46:50

确保您的 after_create :callback 语句位于 has_many/belongs_to 定义之后。

如果它不起作用,您可以创建自己的计数器缓存(无非是调用增量/减量,请参阅 add_counter_cache_callbacks) 并确保在代码之前调用它。

Make sure your after_create :callback statement is after the has_many/belongs_to definition.

If it doesn't work, you can create your own counter cache (it's nothing more than a call to increment/decrement, see add_counter_cache_callbacks) and ensure it's called before your code.

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