diff --git a/db/migrate/20141229025925_change_seems_rateable_rates.rb b/db/migrate/20141229025925_change_seems_rateable_rates.rb index 9ea75e68e..7be2a81fe 100644 --- a/db/migrate/20141229025925_change_seems_rateable_rates.rb +++ b/db/migrate/20141229025925_change_seems_rateable_rates.rb @@ -1,23 +1,27 @@ class ChangeSeemsRateableRates < ActiveRecord::Migration def up - SeemsRateableRates.where("rateable_type = 'HomeworkAttach'").each do |rate| - homework = HomeworkAttach.find_by_id rate.rateable_id - if homework - course = homework.bid.courses.first - if course - teachers = [] - course.members.each do |m| - teachers << m.user_id if m && m.user && m.user.allowed_to?(:as_teacher,course) - end - if teachers.include?(rate.rater_id) - rate.is_teacher_score = 1 - rate.save + Course.all.each do |course| + teachers = [] + course.members.each do |m| + teachers << m.user_id if m && m.user && m.user.allowed_to?(:as_teacher,course) + end + course.homeworks.each do |bid| + bid.homeworks.each do |homework| + SeemsRateableRates.where("rateable_type = 'HomeworkAttach' and rateable_id = #{homework.id}").each do |rate| + if teachers.include?(rate.rater_id) + rate.is_teacher_score = 1 + rate.save + end + end end end end - end end def down + SeemsRateableRates.where("is_teacher_score = 1").each do |rate| + rate.is_teacher_score = 0 + rate.save + end end end