开发者

counter_cache is stale in after_create hook

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜