class AddCourseidToNews < ActiveRecord::Migration
  #迁移原课程通知数据
  def change
    add_column :news, :course_id, :int

    # course_id不能直接设置为  project_id
    #News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all("course_id = project_id")
    #News.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all(project_id: -1)

    News.all.each do |news|
      project = Project.find_by_id(news.project_id)
      if project && project.project_type == 1
        news.course_id = project.course_extra.id
        news.project_id = -1
        news.save(:validate => false)
      end
    end



  end
end
