I have to destroy a big list of data, and update my counter cache, but when I user destroy_all counter cache is called at every delete. Is there any way to update my counter only after the destroy_all?
I already tried ActiveModel Callbacks, but they are the same as destroy_all, called one time for each record.
belongs_to :research, counter_cache: :total_participants
ResearchParticipant.from_research(@research_id).destroy_all
You can use
delete_allwhich doesn't instantiate objects. Meaning callbacks aren't called. Then reset the cache withreset_counters.If you depend on callbacks that must be triggered this might not be an option.