beast rev 2066
[beast-modified.git] / db / migrate / 040_fix_last_posts.rb
blob4324c995a91e452db4b08bf7c9d9c1174aa8c8ec
1 class FixLastPosts < ActiveRecord::Migration
2   class Topic < ActiveRecord::Base
3     has_many :posts, :order => 'posts.created_at'
4   end
5   class Post  < ActiveRecord::Base; end
7   def self.up
8     Topic.find(:all, :include => :posts).each do |topic|
9       post = topic.posts.last
10       Topic.transaction do
11         Topic.update_all(['replied_at = ?, replied_by = ?, last_post_id = ?', 
12           post.created_at, post.user_id, post.id], ['id = ?', topic.id]) if post
13       end
14     end
15   end
17   def self.down
18   end
19 end