diff --git a/app/controllers/homework_commons_controller.rb b/app/controllers/homework_commons_controller.rb index 5d1733983..eb3576f24 100644 --- a/app/controllers/homework_commons_controller.rb +++ b/app/controllers/homework_commons_controller.rb @@ -37,8 +37,6 @@ class HomeworkCommonsController < ApplicationController @category = @main_category.course_second_categories.find_by(id: params[:category]) tip_exception("子目录id有误") if !@category.present? @homework_commons = @homework_commons.where(course_second_category_id: params[:category]) - elsif @homework_type == 4 - @homework_commons = @homework_commons end @all_count = @homework_commons.size diff --git a/app/models/course.rb b/app/models/course.rb index b7bea6ec0..baed1efe6 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -32,7 +32,12 @@ class Course < ApplicationRecord has_many :teacher_course_members, -> { teachers_and_admin }, class_name: 'CourseMember' has_many :teacher_users, through: :teacher_course_members, source: :user has_many :course_messages, dependent: :destroy + has_many :homework_commons, dependent: :destroy + has_many :normal_homeworks, -> { normals }, class_name: 'HomeworkCommon' + has_many :group_homeworks, -> { groups }, class_name: 'HomeworkCommon' + has_many :practice_homeworks, -> { practices }, class_name: 'HomeworkCommon' + has_many :homework_group_settings has_many :graduation_works, dependent: :destroy diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index f198a7143..e510ca666 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -48,6 +48,9 @@ class HomeworkCommon < ApplicationRecord scope :search_homework_type, lambda {|num| where(homework_type:num)} scope :unified_setting, -> {where("unified_setting = ? ", 1)} + scope :normals, -> {where(homework_type: %i[normal])} + scope :groups, -> {where(homework_type: %i[group])} + scope :practices, -> {where(homework_type: %i[practice])} # 是否显示参考答案 def view_answer identity, user_id diff --git a/db/migrate/20190820021047_migrate_course_task_position.rb b/db/migrate/20190820021047_migrate_course_task_position.rb new file mode 100644 index 000000000..cf1b633bd --- /dev/null +++ b/db/migrate/20190820021047_migrate_course_task_position.rb @@ -0,0 +1,13 @@ +class MigrateCourseTaskPosition < ActiveRecord::Migration[5.2] + def change + add_column :homework_commons, :position, :integer, :default => 0 + + Course.find_each do |course| + puts course.id + course.practice_homeworks.order("IF(ISNULL(homework_commons.publish_time),0,1), homework_commons.publish_time DESC, + homework_commons.created_at DESC").reverse.each_with_index do |homework, index| + homework.update_columns(position: index + 1) + end + end + end +end