]> git.openstreetmap.org Git - rails.git/blob - db/migrate/20121005195010_add_diary_entry_counter_caches.rb
Merge remote-tracking branch 'upstream/pull/2833'
[rails.git] / db / migrate / 20121005195010_add_diary_entry_counter_caches.rb
1 class AddDiaryEntryCounterCaches < ActiveRecord::Migration[4.2]
2   class DiaryEntry < ApplicationRecord
3   end
4
5   class User < ApplicationRecord
6   end
7
8   def self.up
9     add_column :users, :diary_entries_count, :integer, :null => false, :default => 0
10
11     DiaryEntry.group(:user_id).pluck(:user_id).each do |user_id|
12       User.reset_counters(user_id, :diary_entries)
13     end
14   end
15
16   def self.down
17     remove_column :users, :diary_entries_count
18   end
19 end