문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top