class UpdateScoreToHomeworkattaches < ActiveRecord::Migration
  def up
    sql = ("UPDATE homework_attaches set is_teacher_score = 1,
													score = (SELECT AVG(seems_rateable_rates.stars) FROM seems_rateable_rates
																	WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id
			AND is_teacher_score = 1 GROUP BY rateable_id);")
    sql_student = ("UPDATE homework_attaches set is_teacher_score = 0,
													score = (SELECT AVG(seems_rateable_rates.stars) FROM seems_rateable_rates
																	WHERE rateable_type = 'HomeworkAttach' AND rateable_id = homework_attaches.id
			AND is_teacher_score = 0 GROUP BY rateable_id) WHERE homework_attaches.score = 0 OR homework_attaches.score is NULL
")
    execute(sql)
    execute(sql_student)
  end

  def down
  end
end
