diff --git a/app/models/course.rb b/app/models/course.rb index e4ade6e9f..a2f5add51 100644 --- a/app/models/course.rb +++ b/app/models/course.rb @@ -165,7 +165,7 @@ class Course < ApplicationRecord all_course_module_types.each do |type| hidden_value = course_module_types.include?(type) ? 0 : 1 - course_module = get_course_module_by_type(type, self.id) + course_module = course_modules.where(module_type: type).first course_module.update_attribute(:hidden, hidden_value) if course_module.present? end end @@ -174,8 +174,9 @@ class Course < ApplicationRecord %w[activity shixun_homework common_homework group_homework graduation exercise poll attachment board course_group] end - def get_course_module_by_type(type, course_id) - CourseModule.where(course_id: course_id, module_type: type).first + def get_course_module_by_type(type) + #CourseModule.where(course_id: course_id, module_type: type).first + self.course_modules.where(module_type: type).first end # 创建课程讨论区 diff --git a/app/models/homework_common.rb b/app/models/homework_common.rb index d67b6e0fd..c3bfd2dd9 100644 --- a/app/models/homework_common.rb +++ b/app/models/homework_common.rb @@ -170,7 +170,7 @@ class HomeworkCommon < ApplicationRecord #删除时更新题库中的引用数 def update_homework_bank_quotes - old_banks = HomeworkBank.where(homework_common_id: self.id) + old_banks = self.homework_bank unless old_banks.blank? old_banks.each do |bank| bank.update_attributes(quotes: (bank.quotes - 1) > 0 ? (bank.quotes - 1) : 0, homework_common_id: nil) @@ -243,10 +243,15 @@ class HomeworkCommon < ApplicationRecord end def min_group_publish_time - HomeworkGroupSetting.where("homework_common_id = #{self.id} and publish_time is not null").pluck(:publish_time).min + #HomeworkGroupSetting.where("homework_common_id = #{self.id} and publish_time is not null").pluck(:publish_time).min + # 可以使用includes + self.homework_group_settings.where("publish_time is not null").pluck(:publish_time).min end def max_group_end_time - HomeworkGroupSetting.where("homework_common_id = #{self.id} and end_time is not null").pluck(:end_time).max + #HomeworkGroupSetting.where("homework_common_id = #{self.id} and end_time is not null").pluck(:end_time).max + # 可以使用includes + self.homework_group_settings.where("end_time is not null").pluck(:end_time).max + end end