class AddRootId < ActiveRecord::Migration def change add_column :messages, :root_id, :integer, :default => nil add_column :journals_for_messages, :root_id, :integer, :default => nil add_column :blog_comments, :root_id, :integer, :default => nil add_column :org_document_comments, :root_id, :integer, :default => nil add_index :messages, :root_id add_index :journals_for_messages,:root_id add_index :blog_comments,:root_id add_index :org_document_comments,:root_id def get_base_parent comment comm = comment while comm.parent comm = comm.parent end comm end count = Message.all.count / 30 + 2 transaction do for i in 1 ... count do i Message.page(i).per(30).each do |message| unless message.parent.nil? parent = get_base_parent message message.update_column('root_id', parent.id) end end end end count = JournalsForMessage.all.count / 30 + 2 transaction do for i in 1 ... count do i JournalsForMessage.page(i).per(30).each do |message| if message.jour_type == "HomeworkCommon" || message.jour_type == "Syllabus" message.update_column('root_id', message.jour_id) else unless message.parent.nil? parent = get_base_parent message message.update_column('root_id', parent.id) end end end end end count = BlogComment.all.count / 30 + 2 transaction do for i in 1 ... count do i BlogComment.page(i).per(30).each do |message| unless message.parent.nil? parent = get_base_parent message message.update_column('root_id', parent.id) end end end end count = OrgDocumentComment.all.count / 30 + 2 transaction do for i in 1 ... count do i OrgDocumentComment.page(i).per(30).each do |message| unless message.parent.nil? parent = get_base_parent message message.update_column('root_id', parent.id) end end end end end end