Rails counter_cache not updating correctly

前端 未结 5 761
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-04 00:00

Using Rails 3.1.3 and I\'m trying to figure out why our counter caches aren\'t being updated correctly when changing the parent record id via update_attributes.

         


        
5条回答
  •  一个人的身影
    2021-02-04 00:33

    If your counter has been corrupted or you've modified it directly by SQL, you can fix it.

    Using:

    ModelName.reset_counters(id_of_the_object_having_corrupted_count, one_or_many_counters)
    

    Example 1: Re-compute the cached count on the post with id = 17.

    Post.reset_counters(17, :comments)
    

    Source

    Example 2: Re-compute the cached count on all your articles.

    Article.ids.each { |id| Article.reset_counters(id, :comments) }
    

提交回复
热议问题