Sha256: e99d07c7f14753ed9a256a3e7261c87dc7f5bab862cf9f91cf7c372487fd13fe

Contents?: true

Size: 988 Bytes

Versions: 30

Compression:

Stored size: 988 Bytes

Contents

class Bare33Content < ActiveRecord::Base
  include BareMigration

  def count_children_of_type(type)
    self.class.find(:all,
                    :conditions => ["article_id = ? and type = ?",
                                               self.id,       type ]).size
  end

  def correct_counts
    self.comments_count   = self.count_children_of_type('Comment')
    self.trackbacks_count = self.count_children_of_type('Trackback')
  end
end


class AddCountCaching < ActiveRecord::Migration
  def self.up
    STDERR.puts "Adding comments_count, trackbacks_count"
    modify_tables_and_update([:add_column, Bare33Content, :comments_count, :integer],
                             [:add_column, Bare33Content, :trackbacks_count, :integer]) do |a|
      if not $schema_generator
        a.correct_counts
      end
    end
  end

  def self.down
    STDERR.puts "Removing counts columns"
    remove_column :contents, :comments_count
    remove_column :contents, :trackbacks_count
  end
end

Version data entries

30 entries across 30 versions & 1 rubygems

Version Path
typo-5.5 db/migrate/033_add_count_caching.rb
typo-5.4.4 db/migrate/033_add_count_caching.rb
typo-5.4.3 db/migrate/033_add_count_caching.rb
typo-5.4.2 db/migrate/033_add_count_caching.rb
typo-5.4.1 db/migrate/033_add_count_caching.rb
typo-5.4 db/migrate/033_add_count_caching.rb
typo-3.99.0 db/migrate/033_add_count_caching.rb
typo-3.99.2 db/migrate/033_add_count_caching.rb
typo-3.99.1 db/migrate/033_add_count_caching.rb
typo-3.99.3 db/migrate/033_add_count_caching.rb
typo-3.99.4 db/migrate/033_add_count_caching.rb
typo-4.0.0 db/migrate/033_add_count_caching.rb
typo-4.0.1 db/migrate/033_add_count_caching.rb
typo-4.0.2 db/migrate/033_add_count_caching.rb
typo-4.0.3 db/migrate/033_add_count_caching.rb
typo-4.1.1 db/migrate/033_add_count_caching.rb
typo-5.0.1 db/migrate/033_add_count_caching.rb
typo-5.0.2 db/migrate/033_add_count_caching.rb
typo-4.1 db/migrate/033_add_count_caching.rb
typo-5.0.3.98.1 db/migrate/033_add_count_caching.rb