class AddBoardsTypeToBoards < ActiveRecord::Migration
  def change
    add_column :boards, :course_id, :int

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

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

  end
end
