journalduhacker/db/migrate/20140109034338_move_comment_counts_to_story.rb
joshua stein d578482e3b move story comment counts out of keystore
it would be nice to use AR's built-in counter cache, but the
comments count has to reflect something custom so stick with what
was there
2014-01-08 22:20:56 -06:00

26 lines
571 B
Ruby

class MoveCommentCountsToStory < ActiveRecord::Migration
def up
add_column :stories, :comments_count, :integer, :default => 0,
:null => false
Keystore.transaction do
Story.lock(true).select(:id).each do |s|
s.update_comments_count!
end
Keystore.where(
Keystore.arel_table[:key].matches("story:%:comment_count")).delete_all
end
end
def down
Keystore.transaction do
Story.select(:id).each do |s|
s.update_comments_count!
end
end
remove_column :stories, :comments_count
end
end