048_remove_count_caching.rb
上传用户:netsea168
上传日期:2022-07-22
资源大小:4652k
文件大小:1k
源码类别:

Ajax

开发平台:

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