ruby-on-railsruby-on-rails-4counter-cacheacts-as-paranoid

Update a counter cache column after restoring a record with paranoia (acts_as_paranoid)


I have a counter_cache column in my model. I use acts_as_paranoid for this model (Paranoia gem). How to update a counter cache column for an associated record when I restore a record?


Solution

  • You may use a before_restore callback. Place .increment_counter method for an associated record inside this before_restore callback.

    reset_counters method or += 1 won't work.