048_remove_count_caching.rb
Upload User: netsea168
Upload Date: 2022-07-22
Package Size: 4652k
Code Size: 1k
Category:

Ajax

Development Platform:

Others

  1. class RemoveCountCaching < ActiveRecord::Migration
  2.   class Content < ActiveRecord::Base
  3.     include BareMigration
  4.     def count_children_of_type(type)
  5.       self.class.find(:all,
  6.                       :conditions => ["article_id = ? and type = ?",
  7.                                                   self.id,     type]).size
  8.     end
  9.     def correct_counts
  10.       self.comments_count = self.count_children_of_type('Comment')
  11.       self.trackbacks_count = self.count_children_of_type('Trackback')
  12.     end
  13.   end
  14.   def self.up
  15.     remove_column :contents, :comments_count
  16.     remove_column :contents, :trackbacks_count
  17.   end
  18.   def self.down
  19.     modify_tables_and_update(
  20.      [:add_column, Content, :comments_count, :integer],
  21.      [:add_column, Content, :trackbacks_count, :integer]) do |a|
  22.       if not $schema_generator
  23.         a.correct_counts
  24.       end
  25.     end
  26.   end
  27. end