1 # frozen_string_literal: true
3 class AddNotesAndDiaryCommentsCounterCaches < ActiveRecord::Migration[7.1]
5 add_column :users, :diary_comments_count, :integer, :default => 0
6 add_column :users, :note_comments_count, :integer, :default => 0
8 users_with_diary_comments = DiaryComment.distinct.pluck(:user_id)
9 users_with_diary_comments.each do |user_id|
10 User.reset_counters(user_id, :diary_comments)
13 users_with_note_comments = NoteComment.where.not(:author_id => nil).distinct.pluck(:author_id)
14 users_with_note_comments.each do |author_id|
15 User.reset_counters(author_id, :note_comments)
20 remove_column :users, :diary_comments_count
21 remove_column :users, :note_comments_count