开发者

counter_cache is stale in after_create hook

开发者 https://www.devze.com 2022-12-20 13:19 出处:网络
I rely on a counter cache value in an after_create hook of my model. However, my hook is called before the counter cach开发者_Python百科e gets updated, thus breaking a computation.

I rely on a counter cache value in an after_create hook of my model. However, my hook is called before the counter cach开发者_Python百科e 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号