class AttachmentProjectCountUpdate < ActiveRecord::Migration def up Project.all.each do |project| unless project.attachments.count.to_i == 0 attachment_count = Attachment.find_by_sql("SELECT * FROM `attachments` where container_id = #{project.id} and container_type ='Project'").count project_score = ProjectScore.where("project_id=?", project.id).first if project_score.nil? ProjectScore.create(:project_id => project.id, :attach_num => 0) else project_score.attach_num = attachment_count project_score.save end end end end def down end end