38 lines
1.8 KiB
Ruby
38 lines
1.8 KiB
Ruby
class AddAttachmentsToCourses < ActiveRecord::Migration
|
|
def change
|
|
Course.where(:id => CourseActivity.where("updated_at > '2017-06-01 00:00:00'").map(&:course_id).uniq).each do |course|
|
|
attachments = Attachment.where(:container_type => 'Course', :container_id => course.syllabus.courses.map(&:id))
|
|
attachments.each do |attachment|
|
|
if Attachment.where(:container_type => 'Course', :container_id => course.id, :filename => attachment.filename).count == 0
|
|
attach_copied_obj = attachment.copy
|
|
attach_copied_obj.tag_list.add(attachment.tag_list) # tag关联
|
|
attach_copied_obj.container = course
|
|
attach_copied_obj.created_on = attachment.created_on
|
|
attach_copied_obj.author_id = attachment.author_id
|
|
attach_copied_obj.copy_from = attachment.copy_from.nil? ? attachment.id : attachment.copy_from
|
|
attach_copied_obj.is_publish = 1
|
|
#attach_copied_obj.publish_time = Time.now + 30.days
|
|
if attach_copied_obj.attachtype == nil
|
|
attach_copied_obj.attachtype = 4
|
|
end
|
|
attach_copied_obj.save
|
|
quo_attachments = Attachment.find_by_sql("select * from attachments where copy_from = #{attach_copied_obj.copy_from} or id = #{attach_copied_obj.copy_from}")
|
|
result = Attachment.find_by_sql("select count(*) as number from attachments where copy_from = #{attach_copied_obj.copy_from}")
|
|
if result.nil? || result.count <= 0
|
|
count = 0
|
|
else
|
|
count = result[0].number
|
|
end
|
|
attach_copied_obj.quotes = count
|
|
attach_copied_obj.save
|
|
quo_attachments.each do |att|
|
|
att.quotes = attach_copied_obj.quotes
|
|
att.save
|
|
end
|
|
#update_quotes attach_copied_obj
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|