Override update_counters from counter_cache: true

0

class Post < ApplicationRecord
  belongs_to :author, counter_cache: true
end

class Author < ApplicationRecord
  has_many :posts

  def self.update_counters(id, counters)
    Author.find(id).do_something!
    super(id, counters) # continue on with the normal update_counters flow.
  end
end

https://stackoverflow.com/questions/5918404/how-can-i-invoke-the-after-save-callback-when-using-counter-cache

  • 0 posts
  • 0 subtopics
  • 3 months ago by vince