1 class AddNotesAndDiaryCommentsCounterCaches < ActiveRecord::Migration[7.1]
2 class DiaryComment < ApplicationRecord
5 class NoteComment < ApplicationRecord
8 class User < ApplicationRecord
12 add_column :users, :diary_comments_count, :integer, :default => 0
13 add_column :users, :note_comments_count, :integer, :default => 0
15 users_with_diary_comments = DiaryComment.distinct.pluck(:user_id)
16 users_with_diary_comments.each do |user_id|
17 User.reset_counters(user_id, :diary_comments)
20 users_with_note_comments = NoteComment.where.not(:author_id => nil).distinct.pluck(:author_id)
21 users_with_note_comments.each do |author_id|
22 User.reset_counters(author_id, :note_comments)
27 remove_column :users, :diary_comments_count
28 remove_column :users, :note_comments_count